Use of $on, $emit and $broadcast

Source: Internet
Author: User
Tags emit

    • $emit can only pass event and data to the parent controller ($emit (name, args))
    • $broadcast can only pass event and data to Child controller ($broadcast (name, args))
    • $on for receiving event and data ($on (name, listener))
<DivNg-controller= "Parentctrl">              <!--Parent Level -  <DivNg-controller= "Selfctrl">              <!--own -    <aNg-click= "click ()">Click Me</a>    <DivNg-controller= "Childctrl"></Div>     <!--sub-level -  </Div>  <DivNg-controller= "Broctrl"></Div>         <!--Lateral -</Div>
varApp = Angular.module (' myApp '), []); App.controller (' Selfctrl ',function($scope) {$scope. Click=function() {$scope. $broadcast (' To-child ', ' child '); $scope. $emit (' To-parent ', ' parent '); }}); App.controller (' Parentctrl ',function($scope) {$scope. $on (' To-parent ',function(event,data) {Console.log (' Parentctrl ', data);//The parent can get the value  }); $scope. $on (' To-child ',function(event,data) {Console.log (' Parentctrl ', data);//children cannot get a value  });}); App.controller (' Childctrl ',function($scope) {$scope. $on (' To-child ',function(event,data) {Console.log (' Childctrl ', data);//The child can get the value  }); $scope. $on (' To-parent ',function(event,data) {Console.log (' Childctrl ', data);//The parent does not get a value  });}); App.controller (' Broctrl ',function($scope) {$scope. $on (' To-parent ',function(event,data) {Console.log (' Broctrl ', data);//no value on the same lateral.  }); $scope. $on (' To-child ',function(event,data) {Console.log (' Broctrl ', data);//no value on the same lateral.  });});

The event arguments in the $on method, the properties and methods of their objects are as follows

Event Properties Purpose
Event.targetscope Emit or propagate the scope of the original event
Event.currentscope The scope of the event currently being processed
Event.name Event name
Event.stoppropagation () A function to prevent further propagation (bubbling/trapping) of events (this applies only to events emitted using ' $emit ')
Event.preventdefault () This method does not actually do anything, but will set ' defaultprevented ' to true. It does not check the value of ' defaultprevented ' until the event listener's implementation takes action.
event.defaultprevented True if ' preventdefault ' is called

Use of $on, $emit and $broadcast

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.