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?
- <div ng-controller="Parentctrl">//Parent
- <div ng-controller="Selfctrl">//own
- <a ng-click="click ()">click me</a>
- <div ng-controller="Childctrl"></div>//Sub-level /c2>
- </div>
- <div ng-controller="Broctrl"></div>//Peer
- </div>
2,js Code
View copy print?
- 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); //parent can get value
- });
- $scope. $on (' to-child ', function (d,data) {
- Console.log (data); //Sub-level not getting a value
- });
- });
- Phonecatcontrollers.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
- });
- });
- Phonecatcontrollers.controller (' Broctrl ', function ($scope) {
- $scope. $on (' to-parent ', function (d,data) {
- Console.log (data); //No value at Peer
- });
- $scope. $on (' to-child ', function (d,data) {
- Console.log (data); //No value at Peer
- });
- });
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.
Angularjs $broadcast $emit $on event triggers a value transfer between controllers