Home > php教程 > PHP开发 > body text

angular ngClick prevents bubbling using default behavior

高洛峰
Release: 2016-12-08 09:40:01
Original
1417 people have browsed it

The example in this article describes how angular ngClick prevents bubbling and uses the default behavior. Share it with everyone for your reference, the details are as follows:

This is actually a very simple question. If you have carefully looked at Angular’s ​​official API documentation, you didn’t want to record it. But this question has been asked more than once, so I will record it here today.

In Angular, a variable called $event has been added to some ng events such as ngClick, ngBlur, ngCopy, ngCut, ngDblclick...

As ngClick is described in the official document:

Expression to evaluate upon click. (Event object is available as $event)

Looking at the Angular code ngEventDirs.js:

var ngEventDirectives = {};
forEach(
   'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
   function(name) {
    var directiveName = directiveNormalize('ng-' + name);
    ngEventDirectives[directiveName] = ['$parse', function($parse) {
     return {
      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});
         });
        });
       };
      }
     };
    }];
   }
);
Copy after login

In the above code we can get two pieces of information:

①. Events supported by Angular: click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste

②. When Angular executes the event function, it passes in a constant named $event, which represents the current event object. If you introduce it before Angular If you have jQuery, then this is jQuery's event.

So we can use the event's stopPropagation to prevent the event from bubbling: The following code: jsbin

html Code:

<!DOCTYPE html>
<html id="ng-app" ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="demo as d">
<div ng-click="d.click(&#39;parent&#39;,$event)">
given some text for click
<hr>
<input type="checkbox" ng-model="d.stopPropagation" />Stop Propagation ?
<hr>
<button type="button" ng-click="d.click(&#39;button&#39;,$event)">button</button>
</div>
</body>
</html>
Copy after login

js Code:

angular.module("app",[])
.controller("demo",[function(){
 var vm = this;
 vm.click = function(name,$event){
  console.log(name +" -----called");
  if(vm.stopPropagation){
   $event.stopPropagation();
  }
 };
 return vm;
}]);
Copy after login

You can check the effect in jsbin.

First open your console, and then click the button without selecting Stop Propagation. You will see two log records. On the contrary, after selecting it, only the log information of the button will appear.

I hope this article will be helpful to everyone in AngularJS programming.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template