Angularjs - Custom指令无法访问控制器范围

时间:2015-05-30 20:24:19

标签: angularjs scope code-injection directive

我正在使用Angular JS构建单页面应用程序。问题是自定义指令mySlider无法访问我分配给模板的控制器的范围。有没有办法解决这个问题?这是我的文件

的index.html

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

分音/ dishestemplate.html

<div my-slider></div>

指令/ 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());
            }
        };
});

2 个答案:

答案 0 :(得分:1)

我终于找到了问题所在。在DOM完成渲染之前调用滑块对象,因此滑块不显示任何内容。我使用$ timeout来调用滑块后渲染。我是这样做的

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());  
        });
    }
};

});

答案 1 :(得分:0)

由于该指令未被隔离,您可以访问所有控制器&#39;指令link函数中的范围变量。

您的代码正常运行。正如@Rhumbori评论http://jsfiddle.net/w5mh927p/完全正常工作。

尝试在link函数console.log(scope)中添加一行,并检查开发人员工具控制台中范围内的所有内容。 https://www.dropbox.com/s/q3tmgapcxn8e71p/Screenshot%202015-05-31%2002.17.33.png?dl=0