I need to pass in a value from a parent directive (bound to controller X) to a child directive, update it, then pass it back all the way to the controller X updateItem
function.
This causes the parent controller X to receive the updated value, which triggers a new $digest
loop, and passes the item down through the chain, and so on. This causes an infinite loop (discussed here).
I need to update the value from the child directive though, so how can I avoid re-triggering the $digest
cycle once the controller's $scope.items
is updated?
Parent controller X:
$scope.updateItem = function(item) {
$scope.items = item;
};
Parent directive template: Bound to parent controller
<div>
<custom-phrases item-to-update="item" update-item="updateItem"></custom-phrases>
</div>
Parent directive 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
};
});
Child directive 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);
...
If I change to {{ item }}
:
<div>
<custom-phrases item-to-update="{{ item }}" update-item="updateItem"></custom-phrases>
</div>
And pass it into the child directive, it comes through as a string and not an object.
EDIT 2:
If I simply update items
in the child directive, like so:
scope.items = {
test: 'testing 123'
};
And maintain 2-way binding in the child directive:
items: '=',
scope.items
is never updated at the parent directive and controller level.