---
title: "Angular.js callback function inside directive attr defined in different attr"  
description: "Angular.js callback function inside directive attr defined in different attr"  
author: "Anonymous User"  
published: 2013-04-09  
updated: 2013-04-09  
canonical: https://www.mindstick.com/forum/746/angular-js-callback-function-inside-directive-attr-defined-in-different-attr  
category: "ajax"  
tags: ["ajax"]  
reading_time: 1 minute  

---

# Angular.js callback function inside directive attr defined in different attr

Hi Everyone!\
So I have this directive called say, mySave, it's pretty much just this\
app.directive('mySave', [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)($http) { return function(scope, element, attrs) { element.bind("[click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social)", function() { $http.post('/save', scope.data).success(returnedData) { // [callback](https://www.mindstick.com/articles/152/using-the-callback) defined on my utils [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) here\
// [user defined](https://www.mindstick.com/interview/23047/how-to-create-user-defined-exception) callback here, from my-save-callback perhaps? } }); }});the element itself looks like this\
<[button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) my-save my-save-callback="callbackFunctionInController()">save</button>callbackFunctionInController is for now just\
$scope.callbackFunctionInController = function() { alert("callback");}when I console.log() attrs.mySaveCallback inside my-save directive, it just ives me a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) callbackFunctionInController(), I read somewhere that I should \
$parse this and it would be fine, so I tried to $parse(attrs.mySaveCallback) which gave me back some function, but hardly the one I was looking for, it gave \
me back\
function (a,b){return m(a,b)} What am I [doing wrong](https://answers.mindstick.com/qa/36736/what-are-indians-doing-wrong-on-whatsapp)? Is this [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) flawed from the beginning?\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi John!\
You can try as following line of code\
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.$apply(attrs.mySaveCallback) } }});\
I hope it resolve your problem


---

Original Source: https://www.mindstick.com/forum/746/angular-js-callback-function-inside-directive-attr-defined-in-different-attr

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
