Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a function inside a directive in AngularJS

I have created on-blur directive for user that blurs out from the input field

<input type="text" on-blur="doSomething({{myObject}})">

myObject looks like:

myObject = {a : foo, b : bar ... }

this is how my directive currently looks like:

    myModule.directive('onBlur',function(){
    return {
        restrict: 'A',
        link: function(scope,element,attrs) {
            element.bind('blur',function(){
                console.log('blurrred');
            });

        }
    }
});

How do I execute the function doSomething({{myObject}}) when the blur event is triggered?

I've tried doing something like this that has failed to work:

...
            element.bind('blur',function(){
                console.log('blurrred');
                doSomething(object);
            });
...
like image 468
Alon Avatar asked Jan 23 '26 15:01

Alon


1 Answers

Inside linking function you can call: scope.doSomething(). To evaluate expression you could do: scope.$eval(expression), to get access to scope objects simply use: scope.myObject.

Of course this is only true for directives not working in isolation.

like image 176
ŁukaszBachman Avatar answered Jan 25 '26 10:01

ŁukaszBachman