Angularjs in the father, son, brother between the controller is worth passing

Source: Internet
Author: User
Tags emit

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

<DivNg-controller= "Parentctrl">//Parent<DivNg-controller= "Selfctrl">//Own<aNg-click= "click ()">Click Me</a>          <DivNg-controller= "Childctrl"></Div>//Sub-level</Div>      <DivNg-controller= "Broctrl"></Div>//Peer</Div> 

2,js Code

Phonecatcontrollers.controller (' Selfctrl ',function($scope) {$scope. Click=function() {$scope. $broadcast (' To-child ', ' child '); $scope. $emit (' To-parent ', ' parent ');            }      }); Phonecatcontrollers.controller (' Parentctrl ',function($scope) {$scope. $on (' To-parent ',function(d,data) {console.log (data); //The parent can get the value        }); $scope. $on (' To-child ',function(d,data) {console.log (data); //children cannot get a value        });            }); Phonecatcontrollers.controller (' Childctrl ',function($scope) {$scope. $on (' To-child ',function(d,data) {console.log (data); //The child can get the value        }); $scope. $on (' To-parent ',function(d,data) {console.log (data); //The parent does not get a value        });            }); Phonecatcontrollers.controller (' Broctrl ',function($scope) {$scope. $on (' To-parent ',function(d,data) {console.log (data); //no value on the same lateral.        }); $scope. $on (' To-child ',function(d,data) {console.log (data); //no value on the same lateral.        });  }); 

3. Click on the output of the Me

    Child      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.

Reprint Please specify
Author: submarine Eagle

Angularjs in the father, son, brother between the controller is worth passing

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.