Pass function from controller to directive, using isolated scope, use function parameter from driective
by greatCar
HTML
<!-- https://stackoverflow.com/questions/18378520/angularjs-pass-function-to-directive -->
<div ng-app="dr" ng-controller="testCtrl">
<test color1="color1" update-fn="updateFn(msg)"></test>
</div>
JavaScript
var app = angular.module('dr', []);
app.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function(msg) {
alert(msg);
}
});
app.directive('test', function() {
return {
restrict: 'E',
scope: {
color1: '=',
updateFn: '&'
},
template: "<button ng-click='updateFn({msg:\"Hello World!\"})'>Click</button>",
replace: true,
link: function(scope, elm, attrs) {
}
}
});