1

I am building a single page app with Angular JS. The problem is the custom directive mySlider cannot access the scope of the controllers I assign to the template. Is there a way to solve this problem? Here are my files

index.html

<script src="js/dishesapp.js"></script>
<div ng-view></div>

partials/dishestemplate.html

<div my-slider></div>

directives/slider.html

<div id="dishsliderholder">
    <ul class="bxslider">
        <li class="dishdetails" ng-repeat="dish in dishes">
            <div class="item">                               
                <div class="title">{{dish.name}}</div>                  
                <div class="description">{{dish.description}}</div>                               
            </div>
        </li>     
    </ul>
</div>

dishesapp.js

var dishesApp = angular.module('dishesApp', ['ngRoute']);
    dishesApp.config(function ($routeProvider) {
    $routeProvider
            .when('/', {templateUrl: 'partials/default.html'})
            .when('/Noodles', {templateUrl: 'partials/dishtemplate.html', controller: 'NoodlesCtrl'})
            .when('/Rolls', {templateUrl: 'partials/dishtemplate.html', controller: 'RollsCtrl'})
            .when('/Pancakes', {templateUrl: 'partials/dishtemplate.html', controller: 'PancakesCtrl'})
            .when('/Rice', {templateUrl: 'partials/dishtemplate.html', controller: 'RiceCtrl'})
            .when('/FamilyStyle', {templateUrl: 'partials/dishtemplate.html', controller: 'FamilyStyleCtrl'})
            .when('/Others', {templateUrl: 'partials/dishtemplate.html', controller: 'OthersCtrl'})
            .otherwise({redirectTo: '/'});
    });

    dishesApp.controller('NoodlesCtrl', function ($scope, $http) {

        $scope.mycategory = "Noodles";
            $http.get("http://www.blabla.com/Data/dishes.php").success(function (response) {
            $scope.dishes = response.records;
        });
    });

dishesApp.controller('RollsCtrl', function ($scope, $http) {

        $scope.mycategory = "Rolls";
            $http.get("http://www.blabla.com/Data/dishes.php").success(function (response) {
            $scope.dishes = response.records;
        });
    });

……………..


    dishesApp.directive('mySlider', function() {
        return {
            restrict: 'A',
            templateUrl: 'directives/slider.html',
            link: function (scope, element, attrs) {              
                    angular.element( document.querySelector('.bxslider')).bxSlider();
                     angular.element( document.querySelector('#dishsliderholder')).css("background-size", getSliderHolderBackgroundSize());
            }
        };
});
3
  • can you produce fiddle/plunker/codepen or code demo here? Commented May 30, 2015 at 20:28
  • Have you set ng-app="dishesApp" on your page? Your code is working for me - jsfiddle.net/w5mh927p Commented May 30, 2015 at 20:36
  • Yes, i did. All the partial views load successfully. Commented May 30, 2015 at 20:38

2 Answers 2

1

I finally figured out the problem. The slider object was called before the DOM finish rendering so the slider shows nothing. I use $timeout to call the slider post render. Here is how I did it

dishesApp.directive('mySlider', function ($timeout) {
return {
    restrict: 'A',
    replace: true,
    templateUrl: 'directives/slider.html',
    link: function (scope, element, attrs) {     

        $timeout(function () {

            angular.element(document.querySelector('.bxslider')).bxSlider();
            angular.element(document.querySelector('#dishsliderholder')).css("background-size", getSliderHolderBackgroundSize());  
        });
    }
};

});

Sign up to request clarification or add additional context in comments.

Comments

0

Since the directive is not isolated, you can access all the controllers' scope variables inside a directive link function.

Your code is working. As @Rhumbori commented http://jsfiddle.net/w5mh927p/ is perfectly working.

Try adding a line inside link function: console.log(scope) and check what all is present on scope in your developer tools console. https://www.dropbox.com/s/q3tmgapcxn8e71p/Screenshot%202015-05-31%2002.17.33.png?dl=0

1 Comment

Feel free to ask any doubts else accept this to be an answer so this question won't appear as unanswered to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.