2014-01-08 54 views

回答

5

未實現的原因是性能。遍歷DOM比檢查每個子分支可能的匹配要快得多。出於這個原因,推薦的方法是讓子元素通知他們的父母其狀態。

請注意,這是通過關聯的控制器實例完成的,而不是通過指令完成的。

我已經與working example

angular.module('foo', []) 

.directive('parentDirective', function() { 
    return { 
    controller: function($scope) { 

     $scope.childs = {}; 

     this.registerChild = function(child) { 
     $scope.childs[child.name] = child; 
     }; 
    }, 
    link: function(scope, element, attrs) {} 
    }; 
}) 

.directive('childDirective', function() { 
    return { 
    controller: function($scope) {}, 
    require: ['^parentDirective', 'childDirective'], 
    link: function($scope, $element, $attrs, $ctrls) { 

     var parent = $ctrls[0]; 
     var child = $ctrls[1]; 

     child.name = $attrs.childDirective; 

     parent.registerChild(child); 
    } 
    }; 
}); 
更新您的普拉克
1

據我所知,Angular允許這樣做,你不能要求一個子指令。你可以只需要從孩子父母的指令,通過

require: '^parentDirectiveName' 

或兄弟姐妹的指令,通過

require: 'siblingDirectiveName' 

所以,是的,這是由設計,或至少缺乏功能。