I need to pass in a value from the parent directive to the child directive, update it, then pass it back. When I pass in the parent function, and create a two-way binding, I get the error:
scope.updateItem is not a function
How can I accomplish this?
Parent directive template:
<div>
<custom-phrases item-to-update="item" update-item="updateItem"></custom-phrases>
</div>
Parent Directive JavaScript:
angular
.module('app')
.directive('itemlist',
function($rootScope, $state) {
return {
restrict: 'EA',
templateUrl: 'directives/cms/itemlist/itemlist.tpl.html',
scope: {
},
link: function(scope, element) {
// This will be called from child directive with updated item
scope.updateItem = function(item) {
console.log('Updating item from itemlist', item);
};
},
};
});
Child Directive JavaScript:
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) {
// do stuff to scope.itemToUpdate...
// then pass it back to parent directive
scope.updateItem(scope.itemToUpdate);
...
Note: I've also tried &
to bind a function:
<div>
<custom-phrases item-to-update="item" update-item="updateItem(item)"></custom-phrases>
</div>
Then in child directive, changed this to:
scope: {
itemToUpdate: '=',
updateItem: '&',
},
If I console log scope.updateItem
I get
TypeError: scope.updateItem is not a function