簡體   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