Angularjs $broadcast $emit $on event triggers a value transfer between controllers

Source: Internet
Author: User
Tags emit vars

Use Angularjs, find the controller between the value of transmission, more troublesome, after a few articles will be said several ways.

One, Angularjs $broadcast $emit $on thought of handling

Within a controller, a method is triggered through an event, in which a variable is defined in a method by $broadcast or $emit, which is obtained through $on in the parent and child controller.

Second, the example illustrates the usage of the Angularjs $broadcast $emit $on

1,html Code

View copy print?
  1. <div ng-controller="Parentctrl">//Parent
  2. <div ng-controller="Selfctrl">//own
  3. <a ng-click="click ()">click me</a>
  4. <div ng-controller="Childctrl"></div>//Sub-level /c2>
  5. </div>
  6. <div ng-controller="Broctrl"></div>//Peer
  7. </div>

2,js Code

View copy print?
  1. Phonecatcontrollers.controller (' Selfctrl ', function ($scope) {
  2. $scope. Click = function () {
  3. $scope.  $broadcast (' to-child ', ' child ');
  4. $scope.  $emit (' to-parent ', ' parent ');
  5. }
  6. });
  7. Phonecatcontrollers.controller (' Parentctrl ', function ($scope) {
  8. $scope. $on (' to-parent ', function (d,data) {
  9. Console.log (data); //parent can get value
  10. });
  11. $scope. $on (' to-child ', function (d,data) {
  12. Console.log (data); //Sub-level not getting a value
  13. });
  14. });
  15. Phonecatcontrollers.controller (' Childctrl ', function ($scope) {
  16. $scope. $on (' to-child ', function (d,data) {
  17. Console.log (data); //child can get value
  18. });
  19. $scope. $on (' to-parent ', function (d,data) {
  20. Console.log (data); //Parent not getting a value
  21. });
  22. });
  23. Phonecatcontrollers.controller (' Broctrl ', function ($scope) {
  24. $scope. $on (' to-parent ', function (d,data) {
  25. Console.log (data); //No value at Peer
  26. });
  27. $scope. $on (' to-child ', function (d,data) {
  28. Console.log (data); //No value at Peer
  29. });
  30. });

3. Click on the output of the Me

    1. Child
    2. Parent

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 $broadcast $emit $on event triggers a value transfer between controllers

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.