【问题标题】:AngularJS directive require parent directive doesn't workAngularJS 指令需要父指令不起作用
【发布时间】:2014-04-30 17:21:27
【问题描述】:

我想用复选框(这是一个递归列表)指令制作一个类别树。

我做了一个名为 categoriesTreeContainer 的指令,其中包含所有类别列表

我做了另一个名为 categoryItem 的指令,其中包含 categoriesTreeContainer

这就是我所做的categoriesTreeContainer

myApp.directive('categoriesTreeContainer', function(){
return {
    restrict : 'E',
    template : '<category-item ng-repeat="category in categoriesTree" category="category"></category-item>',
    scope    : {
        categoriesTree : "=categoriestree",
        selectedCategories : "=ngModel",
    },

    controller : function($scope, $element, $attrs){
        $scope.selectedCategories = [];

        $scope.onSelectionChange = function(category){
            console.log('yah');
        }
    }
}

})

对于categoryItem

myApp.directive('categoryItem', function($compile){
return {
    require: '^categoriesTreeContainer',
    restrict : 'E',
    //replace  : true,
    transclude : true,
    scope    : {
        category : "=category"
    },
    link     : function(scope, element, attrs, categoriesTreeCtrl){
        console.log(categoriesTreeCtrl);

        if(scope.category.subCategories.length>0){

            element.append($compile(
                '<div class="panel panel-default panel-treelist">'+
                '<div class="panel-heading"><h4 class="panel-title">'+
                '<label data-toggle="collapse" data-target="#{{category.$$hashKey}}">'+
                '<input type="checkbox" ng-change="categoriesTreeCtrl.onSelectionChange(category)"  ng-model="category.selected" />'+
                ' {{category.name}}</label></h4></div><div id="{{category.$$hashKey}}" class="panel-collapse collapse">'+
                '<div class="panel-body">'+
                '<category-item id="{{category.$$hashKey}}" ng-repeat="subCategory in category.subCategories" category="subCategory" categoriestree="categoriesTree" ng-model="selectedCategories">'+
                '</category-item></div></div></div>'
                )(scope))
        }else{
            element.append($compile('<label><input ng-change="categoriesTreeCtrl.onSelectionChange(category)" type="checkbox" ng-model="category.selected"> {{category.name}}</label><br/>')(scope));
        }
    }
}

})

在 DOM 中:

<categories-tree-container categoriestree="categoriesTree" ng-model="selectedCategories"></categories-tree-container>

树按照我的意愿渲染。 问题是 categoryItem 指令中的 required 控制器 '^categoriesTreeContainer' 是无效的。我在 link 函数中为categoriesTreeCtrl 做了一个console.log(categoriesTreeCtrl),这就是我得到的:

c {},一个空对象。

我做错了什么?

【问题讨论】:

  • 如果你做一些jsfiddle会更容易尝试。

标签: javascript angularjs controller require directive


【解决方案1】:

categoriesTreeCtrl 将是 void object,因为控制器什么都没有。如果您需要从子指令访问categoriesTreeCtrl.onSelectionChange,则不应将onSelectionChange 作为$scope 的一部分,而是将其定义为控制器的属性。

controller: function($scope, $element, $attrs){
      this.onSelectionChange = function(category){ ... };
 // $scope.onSelectionChange = function(category){...}
}

补充:
子指令上的categoriesTreeCtrl 不等于$scope.categoriesTreeCtrl,这意味着您不能从模板中调用categoriesTreeCtrl。查看ng-change 的值。

【讨论】:

  • @Sn0opr,接受它!这样其他人就会因为知道它解决了手头的问题而从中受益。
  • 我之前做过,但我认为这是一个错误,再做一次:)
猜你喜欢
  • 2013-03-15
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 2013-04-27
  • 2014-12-07
相关资源
最近更新 更多