tha*_*vin 3 angularjs angularjs-directive
这个plunkr引发了这个错误:
Error: [$compile:ctreq] Controller 'childDirective', required by directive 'parentDirective', can't be found!
我可以解决这个问题,但我很好奇这是否是按设计,以及为什么(单亲与多个孩子的事情)?我没有在$ ng.compile文档中看到任何提及此限制的内容.
没有实现的原因是性能.遍历DOM比检查每个子分支以获得可能的匹配要快很多.因此,建议的方法是让子元素通知其父元素其状态.
请注意,这是通过关联的控制器实例完成的,而不是通过指令完成的.
我用一个有效的例子更新了你的插件
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);
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1033 次 |
| 最近记录: |