0

I am trying to define a function in my html in order to use it as a callback for directive. I tried 2 ways:

1. in ng-init

<directive ng-init="foo=function(data){console.log(data);}" callback="foo"></dirctive>


2. in the attribute:
    <directive callback="function(data){console.log(data);}"></directive>

for both i am getting an error.

Is it possible to do it without using the controller?

4
  • @see stackoverflow.com/questions/24640284/angular-directive-callback Commented Sep 24, 2015 at 8:26
  • Hi, I am not sure what you look for. You can create a custom directive to manipulate data with out a controller. Anyway you want to create a module for this custom directive. Commented Sep 24, 2015 at 8:39
  • you should define that function in controller and use that function name on html.. Commented Sep 24, 2015 at 8:39
  • Even though it might be possible it is best practice to keep your logic out of the HTML and inside the controller. The use of ng-init is also generally discouraged (see documentation). Commented Sep 24, 2015 at 8:47

1 Answer 1

0
This way you can call your function and manage your callback
<my-customer >directive</my-customer>
-----------------------------------
angular.module('myapp', [])
.directive('myCustomer', function() {
 return{
  restrict:"E",
  link:function(scope,element,attrs){
        scope.callbackData=function(data,callback){
          var i=30+data;
          callback(i);
        }
        scope.callbackData(50,function(data){
          console.log(data) 
        });  
    } 
 }   
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.