ANGULARJS Development--Communication between controllers

Source: Internet
Author: User

ANGULARJS Development--Communication between controllers

The communication between the instruction and the controller is nothing more than the following methods:

    1. Method based on Scope inheritance
    2. Based on the event propagation method
    3. How the service Works
Method based on Scope inheritance

The simplest way to communicate between controllers is through the inheritance of scope. Assuming that there are two controller parent, Child,child within the parent, the child can be referred to as the Controller parent's sub-controller, which inherits the scope of the parent controller. This allows the child to access all the functions and variables in the scope of the parent.

It is important to note that since the inheritance of scope is based on JS's prototype inheritance, if the variable is of the basic type, then the modification (write) in the child may cause the data in the parent to become dirty. As follows:

Primitive types and inheritance of reference type variables

In this demo code we see two _value, where one of the _value properties is directly registered to the $scope and the other _value is registered to the parent control $scope. obj, the demo effect is as follows:

    1. Child can read to the _value value in the parent, so the default page displays 4 defaults
    2. If you first change the _value property on the $scope directly registered on the child, you register directly with the parent. $scope _value with direct registration in Chile. $scope _value lost contact, on the page: If you click Chil First D button, click on the parent button child. The _value on the $scope will not change.
    3. Conversely, if the _value that is registered directly in the Chile. $scope is not overwritten, it is registered in the parent. The _value of the $scope is linked to the _value of Chile. $scope and the page shows the opposite.
    4. The _value attribute, which is registered on obj, is always associated.

After the above experiments, we come to the conclusion that:

The properties of the child scope override are not registered directly on the $scope object, but should be registered on the reference type on the $scope as much as possible, polluting the $scope.

Based on the event propagation method

Scope inheritance can handle communication problems between parent-child controllers, but it cannot handle communication problems between sibling/neighbor controllers. Communication based on event delivery can solve the problem of parent-child communication. Angular offers three methods: $on, $emit, $broadcast

Child –> Parent: $emit

The event propagation process is this:

    • The controller in child scope registers an up-propagated event by $scope. $emit
    • The event passes through the parent scope of each layer, but each layer of the parent scope does not process
    • If you want to process it, use $scope in the parent scope that you want to work with. $on listening, that's all.

DEMO

As with the DOM event in JS, if you do not want your event to propagate further up, the $on processing function call in e.stopPropagation() .

Parent –> Child: $broadcast

From the parent to the child, with the subset to the parent, using the same $broadcast registration time, with $on listening, DEMO.

Between siblings

The communication between the child scope with the same parent scope, that is, the sibling/neighbor scope, is actually the use of a common parent to deliver the message:

    • Child scope who wants to send a message, $emit one for "Milk dad"?
    • Then through the "Milk Dad" $broadcast give all the children the same information, of course, the one who sends the message can choose whether or not to ignore the information they sent
How to angular services

The service in angular is a singleton that generates an object in the service that can be shared across all controllers using dependency injection. Referring to the following example, a controller modifies the value of the service object and obtains the modified value in another neighboring controller:

DEMO

ANGULARJS Development--Communication between controllers

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.