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