Get return value in callback Angular js Directive
While calling the controller's function from inside a directive with an isolate scope, you need to pass an object:
HTML
<div ng-app="myApp" ng-controller="ctrl"> <div abc-option get-display-name="getDisplayName(columnName)"></div></div>
JS
var app = angular.module('myApp', []);function ctrl($scope){ $scope.getDisplayName = function(columnName) { return 'abc'; };}app.directive('abcOption', function($compile,$timeout) { return { restrict : 'A', template : '<div class="filter-content">abc</div>', replace : true, scope : { callBackMethod:'&getDisplayName' }, link: function(scope,element,attrs){ /* send an object to the function */ console.log(scope.callBackMethod({columnName:"hurray"})); } };});
The answer from CodeHater works but is (just a little) confusing. So I updated it to make it easier to understand
HTML
<div ng-app="myApp" ng-controller="ctrl"> {{returnVal}} <div abc-option callback="setDisplayNameFn(mustBeTheSame)"></div></div>
JS
var app = angular.module('myApp', []);function ctrl($scope){ $scope.setDisplayNameFn = function(whatever) { $scope.returnVal= whatever; };}app.directive('abcOption', function($compile,$timeout) { return { restrict : 'A', template : '<div class="filter-content"><b>directive html<b></div>', replace : true, scope : { callBackMethod:'&callback' }, link: function(scope,element,attrs){ /* send an object to the function */ console.log(scope.callBackMethod({mustBeTheSame:"value from directive"})); } };});