Event broadcast in Angularjs-Comprehensive Analysis of $ broadcast, $ emit, $ on, And angularjsemit
In Angularjs, you can use the event broadcast mechanism of $ broadcast, $ emit, and $ on in combination for communication between different scopes.
Introduction:
$ Broadcast is used to spread events from parent-level scopes to child-level scopes, including itself. Format: $ broadcast (eventName, args)
$ Emit is used to spread events from child-level scope to parent-level scope, including itself, until the root scope. Format: $ emit (eventName, args)
$ On is used to monitor events and corresponding data that are propagated from the child or parent scope in the scope. Format: $ on (event, data)
In the preceding description, eventName is the name of the event to be monitored. The parameter event in the $ on method is the event-related object, and data is the data of event propagation.
The event parameter in the $ on method has the following attributes and methods:
Description of event attributes/methods
Event attributes/methods |
Function Description |
Event.tar getScope |
Obtain the scope of a promotional event |
Event. currentScope |
Obtain the scope of the event to be received |
Event. name |
Name of the event to be propagated |
Event. stopPropagation () |
Prevents event bubbling propagation, which is only valid for $ emit events. |
Event. preventDefault () |
Prevent propagation events |
Event. defaultPrevented |
If the preventDefault event is called, true is returned. |
Code:
<! DOCTYPE html>
Code
Effect:
Other attributes:
The above Angularjs event broadcast-Comprehensive Analysis of $ broadcast, $ emit, $ on is all the content shared by xiaobian, and I hope to give you a reference, we also hope that you can support the customer's home.