Event broadcast in Angularjs-Comprehensive Analysis of $ broadcast, $ emit, $ on, And angularjsemit

Source: Internet
Author: User

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.

Related Article

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.