Detailed use of $ BROADCAST and $ EMIT in Angular

To transfer variables between controllers, the $ BROADCAST and $ EMIT methods in Angular are required to pass, while using $ ON to receive events and respond.

Broadcast translated as broadcast, ie the superior is transmitted.


 Angular.Module ("app", []) .controller "Child", Function ($ Scope) {$ Scope. $ ON ("ParentChange", Function (E, M) {$ Scope.change = "Changed"; $ Scope.child = m;})}) .controller "Parent", Function {$ Scope. $ Watch ("Parent", Function (N, O) {if (n == o) {Return;} $ scope. $ Broadcast ("ParentChange", N) })}})   Parent: {Parent}}   {{child}}   
The above code uses the value of the value of the Parent, "broadcast" "broadcast" out "out of the ParentChange event and pass the parameters (the value of the input box) when changing changes. $ Scope. $ ON ("Parentchange," in the elemental scope."Handler" "to receive the ParentChange event, then accept the value of the input box and assign the Child variable in the sub-scope. This completes the value of the value.
Effect:

EMIT translation is transmitted, namely the lower level.

Example code: Angular中$broadcast和$emit的使用方法详解


Angular.Module (“app”, []) .controller (“Parent”, Function ($ Scope) {$ Scope. $ ON (“Childchange”, Function (E, M) {$ Scope.change = “Changed”; $ scope.parent = m})}) .Controller (“Child”, Function ($ scope) {$ scope. $ watch (“child”, function (n, o) {if (n == o) {RETURN;} $ scope. $ EMIT (“Childchange”, N)}}
 {{{parent}}  child: {{child}}}      
When the Child value of the child element changes, a ChildChange event will be issued to the superior. At this time, the parent element is listening to the $ ON ("ChildChange") will respond, assign the passing parameters to PARENT, and the entire process is similar to Broadcast.
There is a hierarchical relationship in the above-described scope. What should I do for the scope of the same level?
We can use service to broadcast events:


Angular.Module (“app”, []). Service (“MyServive”, Function ($ ROOTSCOPE) {Return {Change: Function (N) {$ Rootscope. $ Broadcast (“ValueChange”, N);}}}) .Controller (“Bro1”, Function ($ Scope, {$ Scope. $ Watch (“Value1”, Function (N) {MyServive.change (N);} $ Scope. $ ON (“ValueChange”, Function (E, M) {Console.log (“Value1 “); $ scope.value1 = m;})}) .Controller (” bro2 “, function ($ scope, myservive) {$ scope. $ watch (” value2 “, function (n) {MyServive.change (n) }) $ SCOPE. $ ON (“ValueChange”, Function (E, M) {Console.log (“Value2”); $ scope.value2 = m

Value1: Angular中$broadcast和$emit的使用方法详解

Value2:

     We used $ rootscope in our service, all of which belong to it, based on this, we can broadcast events in the root scope, $ rootscope. $ Broadcast ("ValueChange", N);, when the value in each sub-field change, we call the Change method in the service to broadcast this event, while each scope is listening to $ ON ("ValueChange" event. And make corresponding processing, so that the value in the sub-scope is passed to the brothers' role.   

The above is all the contents of this article, I hope to help everyone, I hope everyone can support Tumi Clouds. .

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment