Angular Ngclick prevents bubbles from using default behavior _angularjs

Source: Internet
Author: User
Tags button type

The example in this article describes how angular ngclick prevents bubbling from using default behavior. Share to everyone for your reference, specific as follows:

This is actually a very simple question, and if you have taken a serious look at angular's official API documentation, you don't want to record it. But this question has been asked more than once, so it's recorded here today.

In angular there have been some NG events such as Ngclick,ngblur,ngcopy,ngcut,ngdblclick ... A variable called $event is added to the

As Ngclick is described in the official document:

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

After viewing the angular code ngeventdirs.js:

var ngeventdirectives = {};
ForEach (
   ' click DblClick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress s Ubmit focus Blur copy cut Paste '. Split ('),
   function (name) {
    var directivename = directivenormalize (' ng-' + name) ;
    Ngeventdirectives[directivename] = [' $parse ', function ($parse) {return
     {
      compile:function ($element, attr) { C8/>VAR fn = $parse (Attr[directivename]);
       return function (scope, element, attr) {
        element.on (lowercase (name), function (event) {
         scope. $apply ( function () {
          fn (scope, {$event: event});
         });
        };
   };}}
;

In the top code we can get two messages:

①. Angular supported Event:click DblClick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup KEYP Ress submit focus blur copy cut paste

②. Angular passed a constant named $event when executing an event function that represents the current event object, and if you introduced jquery before angular, then this is the event of jquery.

So we can use the stoppropagation of event to block the bubbling of events: the following code: Jsbin

HTML code:

<! DOCTYPE html>
 
 

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;
}];

You can view the effect in Jsbin.

First open your console, and then click on the button without selecting the stop propagation you will see two log records, but only the button log information will appear when selected.

I hope this article will help you to Angularjs program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.