繁体   English   中英

监视自定义指令属性,不改变父父范围的变化

[英]watch on custom directive attribute not changing on parents scope change

我在另一个自定义指令中有一个自定义指令,我希望子指令在父级更改范围值时更改。

父指令的html有一个名为childHeight的范围变量,我有一个设置它的函数。

<button ng-on-click="setHeight(500)"> change the height </button>
<child-directive ctrl-height="childHeight"></child-directive>

child指令在ctrlHeight上有一个监视

scope: {
        ctrlHeight: '=?',
        ctrlWidth: '=?'
    },
link: function (scope, element, attrs) {
        scope.$watchCollection(['ctrlHeight', 'ctrlWidth'], function() {
                scope.reSize(scope.ctrlWidth, scope.ctrlHeight);
        });
    }

当我在setHeight中更改范围变量时

$scope.childHeight = 500;

怎么没有触发手表。

我尝试添加$ apply()或$ digest,这两个都给我一个错误

如果你需要同时观看ctrlHeightctrlWidth ,你可以尝试这样的事情:
scope.$watch('ctrlHeight + ctrlWidth', watcher);
这样,每次每次更改时,都会触发您的watcher fn。 $apply()也不起作用,因为你已经在Angular上下文中了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM