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?

user2436448
  • 445
  • 4
  • 7
  • 18
  • @see http://stackoverflow.com/questions/24640284/angular-directive-callback – Dennis Jaamann Sep 24 '15 at 08: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. – Abhilash Augustine Sep 24 '15 at 08:39
  • you should define that function in controller and use that function name on html.. – Pankaj Parkar Sep 24 '15 at 08: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](https://docs.angularjs.org/api/ng/directive/ngInit)). – muenchdo Sep 24 '15 at 08:47

1 Answers1

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) 
        });  
    } 
 }   
});
Rubel hasan
  • 2,522
  • 1
  • 13
  • 22