Radec
Radec

Reputation: 65

Trigger events on every ng-click

Is there any way to trigger a function from all ng-clicks in an AngularJS application?

I know I can trigger the code by adding it in the functions currently bound to their specific ng-ifs, but if there's some way to trigger it by any ng-click in the controller, this would be useful for me.

Thank you all!

Upvotes: 0

Views: 77

Answers (2)

PRogalla
PRogalla

Reputation: 236

You can override the ngClick directive to trigger a function or broadcast on every click.

The original implementation you have to modify:

compile: function($element, attr) {
  var fn = $parse(attr[directiveName]);
  return function(scope, element, attr) {
    element.on(lowercase(name), function(event) {
      scope.$apply(function() {
        fn(scope, {$event:event});
      });
    });
  };
}

Upvotes: 1

KrishCdbry
KrishCdbry

Reputation: 1059

According to your question. I understood one thing that you want to trigger ng-click() for some items inside your ng-if So if that is the case then use ng-show instead. Because ng-click won't work inside ng-if.

But if you want catch the ng-click() event firing then write ng-click for body but that is not a good implementation.

Hope this helps. Thanks !

Upvotes: 0

Related Questions