我需要从父指令(绑定到控制器X)传递一个值到一个子指令,更新它,然后将它传递回去控制器X updateItem
功能。
这会导致父控制器X接收更新的值,这会触发新的$digest
循环,并将项目向下传递到链中,依此类推。这会导致无限循环(discussed here)。
我需要更新子指令的值,所以一旦控制器的$scope.items
更新,我该如何避免重新触发$digest
周期?
父控制器X:
$scope.updateItem = function(item) {
$scope.items = item;
};
家长指令模板:绑定到父控制器
<div>
<custom-phrases item-to-update="item" update-item="updateItem"></custom-phrases>
</div>
家长指令JS:
angular
.module('app')
.directive('itemlist',
function($rootScope, $state) {
return {
restrict: 'EA',
templateUrl: 'directives/cms/itemlist/itemlist.tpl.html',
scope: {
},
link: function(scope, element) {
//
},
controller: parentControllerX
};
});
儿童指令JS:
angular
.module('app')
.directive('customPhrases',
function($rootScope) {
return {
restrict: 'AE',
scope: {
itemToUpdate: '=',
updateItem: '=',
},
templateUrl: 'directives/cms/customPhrases/custom_phrases_directive.tpl.html',
link: function(scope, element) {
scope.itemToUpdate.attr1 += 1;
scope.itemToUpdate.attr2 += 1;
// then pass it back to parent directive
scope.updateItem(scope.itemToUpdate);
...
如果我更改为{{ item }}
:
<div>
<custom-phrases item-to-update="{{ item }}" update-item="updateItem"></custom-phrases>
</div>
它传递到孩子的指令,它涉及通过一个字符串,而不是一个对象。
编辑2:
如果我只是在孩子指令更新items
,就像这样:
scope.items = {
test: 'testing 123'
};
并保持2路在孩子指令绑定:
items: '=',
scope.items
从不会在父指令和控制器级别更新。
什么是应该启动更新? – Yatrix
该项目在控制器和指令的实例化中通过链传递给子指令。子指令更新值并返回到父指令和控制器 – Growler
因此它只更新一次? – Yatrix