Callback function inside directive attr defined in different attr Callback function inside directive attr defined in different attr angularjs angularjs

Callback function inside directive attr defined in different attr


So what seems like the best way is using the isolated scope as suggested by ProLoser

app.directive('mySave', function($http) {   return {      scope: {        callback: '&mySaveCallback'      }      link: function(scope, element, attrs) {        element.on("click", function() {            $http.post('/save', scope.$parent.data).success(returnedData) {                // callback defined on my utils service here                scope.callback(); // fires alert            }        });      }   }});

For passing parameters back to controller do this

[11:28] <revolunet> you have to send named parameters [11:28] <revolunet> eg my-attr="callback(a, b)" [11:29] <revolunet> in the directive: scope.callback({a:xxx, b:yyy})


There are a lot of ways to go about what you're doing. The FIRST thing you should know is that the $http.post() is going to be called as soon as that DOM element is rendered out by the template engine, and that's it. If you put it inside a repeat, the call will be done for each new item in the repeater, so my guess is this is definitely not what you want. And if it is then you really aren't designing things correctly because the existence of DOM alone should not dictate queries to the backend.

Anyway, directly answering your question; if you read the albeit crappy docs on $parse, it returns you an evaluation expression. When you execute this function by passing the scope to evaluate on, the current state of that expression on the scope you passed will be returned, this means your function will be executed.

var expression = $parse(attrs.mySave);results = expression($scope); // call on demand when neededexpression.assign($scope, 'newValu'); // the major reason to leverage $parse, setting vals

Yes, it's a little confusing at first, but you must understand that a $scope changes constantly in asynchronous apps and it's all about WHEN you want the value determined, not just how. $parse is more useful for a reference to a model that you want to be able to assign a value to, not just read from.

Of course, you may want to read up on creating an isolate scope or on how to $eval() an expression.

$scope.$eval(attrs.mySave);


You can use .$eval to execute a statement in the given scope

app.directive('mySave', function($http) {   return function(scope, element, attrs) {      $http.post('/save', scope.data).success(returnedData) {          // callback defined on my utils service here          // user defined callback here, from my-save-callback perhaps?          scope.$eval(attrs.mySaveCallback)      }   }});

TD: Demo

If you want to share data between a directive and a controller you can use the two way binding

app.controller('AppController', function ($scope) {   $scope.callbackFunctionInController = function() {      console.log('do something')   };   $scope.$watch('somedata', function(data) {      console.log('controller', data);   }, true);});app.directive('mySave', function($http, $parse) {   return {     scope: {       data: '=mySaveData',       callback: '&mySaveCallback' //the callback     },     link: function(scope, element, attrs) {       $http.get('data.json').success(function(data) {         console.log('data', data);         scope.data = data;         scope.callback(); //calling callback, this may not be required       });     }   };});

Demo: Fiddle