Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can angularjs ng-click process events during the capturing phase?

Is it possible to have angularjs ng-click process events during the capturing phase instead of bubbling phase? I want to aggregate data from each of the parent elements in order starting from the parent and ending with the element that was clicked on.

like image 516
dbschofield Avatar asked Aug 13 '14 21:08

dbschofield


People also ask

What is Ng-click function in AngularJS?

The AngularJS ng-click directive facilitates you to specify custom behavior when an element is clicked. So, it is responsible for the result what you get after clicking. It is supported by all HTML elements.

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

What is the use of NG change in AngularJS?

AngularJS ng-change Directive The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

How events are used in AngularJS?

An Events in AngularJS can be used to perform particular tasks, based on the action taken. Both Angular Event & the HTML Event will be executed & will not overwrite with an HTML Event. It can be added using the Directives mentioned below: ng-mousemove: The movement of the mouse leads to the execution of the event.


1 Answers

Lets see the source code of ng-click at ngEventDirs.js#L50

As you can see the ng-click and all other event directives using .on().

So, the answer is No, it is not possible.

If you really need it, you could write a custom directive for that. For example, modify the code of ng-click a bit:

.directive('captureClick', function($parse) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      var fn = $parse(attrs.captureClick);
      return function(scope, element) {
        element[0].addEventListener('click', function(event) {
          scope.$apply(function() {
            fn(scope, {
              $event: event
            });
          });
        }, true);
      };
    }
  }
});

and use it like this:

<div title="A" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
  <div title="B" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
    <div title="C" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
      Yo!
    </div>
  </div>
</div>

Example Plunker: http://plnkr.co/edit/SVPv0fCNRQX4JXHeL47X?p=preview

like image 171
runTarm Avatar answered Oct 26 '22 23:10

runTarm