2

I'm trying to bind angular function to regular js event (logedIn event) using angular directive as described here.

The directive code is:

evApp.directive('onlogin', function () {
return {
    restrict: 'A',
    transclude: true,
    scope: {
        'onlogin': '&'
    },
    link:function (scope, element, attr) {
        //Code to detect when element is pulled goes here
        document.addEventListener("logedIn", function (e) {
            scope.$eval(attr.onlogin);
        });
    }
}
});

The html tag is:

<div  onlogin="loggedIn()"></div>

Its worked but I didn't found a way to pass the parameter 'e' that arrived with the event in the link function. I tried to pass from the html the function handler only and call the function with the parameters from the link function but it didn't worked. some thing like:

<div  onlogin="loggedIn"></div>

Thanks, Amichai

1
  • you should add scope.$apply to let angular now that it needs to fire digest cycle and then just call scope.onlogin({e: e}); Commented May 26, 2016 at 8:04

2 Answers 2

4

you should add scope.$apply to let angular now that it needs to fire digest cycle and then just call scope.onlogin({e: e}) basically you can use it in handler, as you would use $event.

 evApp.directive('onlogin', function () {
return {
    restrict: 'A',
    transclude: true,
    scope: {
        'onlogin': '&'
    },
    link:function (scope, element, attr) {
        //Code to detect when element is pulled goes here
        document.addEventListener("logedIn", function (e) {
            scope.$apply(function () {
             scope.onlogin({e: e})
            });
        });
    }
}
});
Sign up to request clarification or add additional context in comments.

3 Comments

Its give me an error that attr.onlogin is not a function
I changed the attr.onlogin({e:e}); to scope.onlogin({e:e}); and now its working. Thank you.
Yes, you are right. I made mistake when I was copy pasting your code, it should be scope.
1

The call to the onlogin function need to be inside anonymous function in order to add parameters, but the function need to be stored outside the anonymous function, otherwise it will not fire.

evApp.directive('onlogin', function () {
    return {
       restrict: 'A',
       transclude: true,
       scope: {
         'onlogin': '&'
       },
       link:function (scope, element, attr) {
          //Code to detect when element is pulled goes here
          document.addEventListener("logedIn", function (e) {
             var fn = scope.onlogin();
             scope.$apply(function () {
                  fn(e);
             });
          });
       }
    }
});

1 Comment

Yes, you were right to do so, i missed it, cos did copy paste fragment of your code and there you used attr, obviously it should be scope. My mistake by on reading code carefully enough.

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.