Angularjs Learning: Events

Source: Internet
Author: User
Tags emit

The angular event system does not communicate with the browser's event system, which means that we can only listen for angular events instead of DOM events at the scope.

Angular event propagation: On a nested controller scope chain , it is possible to propagate down ($broadcast) and propagate up ($emit).

<div ng-controller= "Parentctrl" >//Parent <div ng-controller= "Selfctrl" >//Own <a ng-click= "click ()" > Click me</a> <div ng-controller= "Childctrl" ></div>//child </div> <div ng-controller= "Broc TRL "></div>//Peer </div>. Controller (' Selfctrl ', function ($scope) {$scope. click = function () {$scope.  $broadcast (' to-child ', ' child ');  $scope. $emit (' to-parent ', ' parent ');  }  }); . Controller (' Parentctrl ', function ($scope) {$scope. $on (' To-parent ', function (d,data) {console.log (data);//parent can get value})  ;  $scope. $on (' To-child ', function (d,data) {console.log (data);//Sub-level not getting value});  });  . Controller (' Childctrl ', function ($scope) {$scope. $on (' To-child ', function (d,data) {console.log (data);//child can get value});  $scope. $on (' To-parent ', function (d,data) {console.log (data);//Parent not getting a value});  });  . Controller (' Broctrl ', function ($scope) {$scope. $on (' To-parent ', function (d,data) {console.log (data);//Not having a value on a peer}); $scope. $on (' To-child ', functiOn (d,data) {console.log (data);//No value on peer}); });
The value assigned by $broadcast can only be obtained by the child, and $emit assigned value can only be obtained by the parent, and nothing at the level can be obtained.

Angularjs Learning: Events

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.