부모 지침에서 자식 지시문을 필요로 할 수없는 이유는 무엇입니까?
-
20-12-2019 - |
문제
이 Plunkr 이 오류가 발생했습니다 :
Error: [$compile:ctreq] Controller 'childDirective', required by directive 'parentDirective', can't be found!
나는 이것을 해결할 수 있지만, 이것이 이것이 - 디자인이라면, 왜 (단일 부모 VS 다중 아이들)가되는지 궁금합니다. $ 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);
}
};
});
. 다른 팁
자녀 지침을 요구할 수는 없습니다.자식에서만 상위 지침이 필요할 수 있습니다.
require: '^parentDirectiveName'
.
또는 형제 지침,
require: 'siblingDirectiveName'
.
그래서 예, 이것은 디자인 또는 적어도 특징이 부족합니다.
제휴하지 않습니다 StackOverflow