myApp.directive('myDirective', function () { return { scope: { controllerFunction: '&callbackFunction' }, link: function (scope, element, attrs) { scope.controllerFunction({ arg: '123' }); }, } });And the markup:
<div ng-controller="MyCtrl as vm"> <span my-directive callback-function="vm.theControllerFunction(arg)" ></span> </div>
We can also call the controller function whenever a value in the directive changes. An example of such directive:
myApp.directive('observeValueChange', function () { return { scope: { controllerFunction: '&callbackFunction' }, link: function (scope, element, attrs, ngModel) { attrs.$observe('theValue', function (newValue) { scope.controllerFunction({ arg: newValue }); }) }, } });The markup:
<input type="text" ng-model="vm.myVariable" /> <span observe-value-change the-value="{{vm.myVariable}}" callback-function="vm.theControllerFunction(arg)" ></span>
See the working example in Plunker.
No comments:
Post a Comment