[英]watch on custom directive attribute not changing on parents scope change
I have a custom directive inside another custom directive and I want the sub directive to change when it's parent changes a scope value. 我在另一个自定义指令中有一个自定义指令,我希望子指令在父级更改范围值时更改。
the html of the parent directive has a scope variable called childHeight and I have a function that sets it. 父指令的html有一个名为childHeight的范围变量,我有一个设置它的函数。
<button ng-on-click="setHeight(500)"> change the height </button>
<child-directive ctrl-height="childHeight"></child-directive>
the child directive has a watch on the ctrlHeight child指令在ctrlHeight上有一个监视
scope: {
ctrlHeight: '=?',
ctrlWidth: '=?'
},
link: function (scope, element, attrs) {
scope.$watchCollection(['ctrlHeight', 'ctrlWidth'], function() {
scope.reSize(scope.ctrlWidth, scope.ctrlHeight);
});
}
When I change the scope variable in the setHeight 当我在setHeight中更改范围变量时
$scope.childHeight = 500;
how come the watch is not triggered. 怎么没有触发手表。
I tried adding an $apply() or $digest both of which give me an error 我尝试添加$ apply()或$ digest,这两个都给我一个错误
If you need to watch both ctrlHeight
and ctrlWidth
, you could try something like this: 如果你需要同时观看
ctrlHeight
和ctrlWidth
,你可以尝试这样的事情:
scope.$watch('ctrlHeight + ctrlWidth', watcher);
That way, everytime each of them changes, your watcher
fn is triggered. 这样,每次每次更改时,都会触发您的
watcher
fn。 Also $apply()
will not work because you're already in Angular context. $apply()
也不起作用,因为你已经在Angular上下文中了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.