[英]How can I bind scope property of transcluded element to parent?
I'm attempting to create a reusable parent container directive. 我正在尝试创建可重用的父容器指令。 What I want is something like the following: 我想要的是以下内容:
<parent title="Parent title">
<child></child>
</parent>
where I can have the child elements change a value in the parent's scope. 我可以在其中让子元素更改父级范围内的值。
I've attempted this binding as follows (see this fiddle ): 我已尝试按以下方式进行绑定(请参阅此小提琴 ):
myApp.directive('parent', function() {
return {
scope: {'title': '@'},
transclude: true,
// Manually transclude so we set parent scope to be this scope.
link: function(scope, element, attrs, ctrl, transclude) {
transclude(scope.$new(), function(content) {
element.append(content);
});
},
restrict: 'EA',
template: '<div><b>PARENT:</b> {{title}}</div>',
controller: function($scope) {
$scope.inherited = true;
$scope.$watch('inherited', function(newValue, oldValue) {
if(!newValue) {
console.log('Parent: inherited switched');
}
});
}
}
});
myApp.directive('child', function() {
return {
restrict: 'EA',
scope:{
inherited: '='
},
template: '<div><b>Child:</b> inherited attribute = {{inherited}}</div>',
controller: function($scope) {
// Why is inherited not bound to parent here?
console.log($scope.inherited);
// It obviously exists in the parent...
console.log($scope.$parent.inherited);
}
}
});
From my understanding of the API , setting an object hash scope with inherited: '='
should bind to the parent property, but as you can see in the fiddle, it doesn't. 从我对API的理解来看,设置具有inherited: '='
的对象哈希范围inherited: '='
应该绑定到父属性,但是正如您在小提琴中看到的那样,它没有绑定。
I have two questions: 我有两个问题:
inherited
not bound to the parent property? 为什么inherited
不绑定到父属性? Caveats: 注意事项:
require: '^parent'
, since this creates a rather strong dependency of the child on the parent (I might want to create a number of possible parent containers and a number of child directives and mix-and-match). 我宁愿不要使用require: '^parent'
,因为这会导致子级对父级的依赖性很强(我可能想创建许多可能的父级容器,许多子级指令以及混合匹配)。 link
in the child directive to climb to the $parent
. 我绝对不会在child指令中使用link
来爬到$parent
。 That's a hack and won't work in general for deeply nested directives. 这是一个hack,对于深度嵌套的指令通常不起作用。 Thanks for any guidance. 感谢您的指导。
You can either do: 您可以执行以下任一操作:
<child inherited="inherited"></child>
where you actually pass in the inherited attribute, since that is what it is looking for when you do 您实际传递继承属性的位置,因为这是您执行操作时要查找的内容
scope:{
inherited: '='
},
Or you could do: 或者您可以这样做:
myApp.directive('child', function() {
return {
restrict: 'EA',
scope: false,
so that it uses it's parent's scope. 以便使用其父级的范围。
Either way, you are likely to run into some issues binding to a primitive. 无论哪种方式,您都可能会遇到绑定到原语的一些问题。 You will probably save yourself a lot of headache by making inherited an object, passing the object around, and when you need to display it show the object property: 通过使继承对象,传递对象以及在需要显示它时显示对象属性,您可能会省去很多麻烦。
JS: JS:
$scope.inherited = {is: true};
HTML: HTML:
{{inherited.is}}
But then keep passing around the whole object. 但是,然后继续绕过整个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.