My requirement is to call child directive function from parent directive. Currently we have implemented publish-subscribe pattern. Child directive subscribe for some event and we make a trigger on parent directive. publish subscribe is implemented through $.callbacks
Other ways this can be done is using broadcast. Or a child directive have a watch on some variable and we can change this variable on parent.
We did not use broadcast because parent directive contains lot of child events and we did not want it to go through all.
There is another way we can implement this is using two way function binding
directives.directive('twoWayBind', function () {
return {
restrict: "E",
transclude: true,
templateUrl: 'app/twoWayBindFunction.html',
scope: {
twoWayBinding: '='
},
controller: 'twoWayBindFunctionController',
bindAsController: true,
controllerAs: 'vm',
link: function (scope, element, attrs, controller) {
}
};
});
controllers.controller('twoWayBindFunctionController', ['$scope', function (scope) {
var vm = this;
vm.scope = scope;
scope.twoWayBinding = function () {
console.log('twoway bind');
}
}]);
we can call twoWayBinding function from parent scope.
I would like to understand what is the best practice.