Nimrod Nimrod - 1 year ago 124
AngularJS Question

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!

Answer Source

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});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download