Ng-controller value replication and reference replication in Angular JS

Source: Internet
Author: User

We know that when using Ng-app or Ng-controller directives, a new scope ($rootScope or $scope) is created, and scopes created with the Ng-controller directive inherit the parent scope ($ Rootscope or $scope) all methods and properties.

But the attributes inherited here have some knowledge.

Run the following code:

Html

<DivNg-controller= "Somecontroller">{{Somebarevalue}}<ButtonNg-click= "someaction ()">parent Button</Button>    <DivNg-controller= "Childcontroller">{{Somebarevalue}}<ButtonNg-click= "childaction ()">child Button</Button>    </Div></Div>

Js

Angular.module (' myApp ', []). Controller (function($scope) { $scope. somebarevalue = ' Hello C '    Omputer ';     function() { $scope. somebarevalue = ' Hello Human, from parent ';  }; }). controller (functionfunction() { $scope. somebarevalue = ' Hello human,    From child ';  }; });

Parent-Child scopes display the same value after initial loading

When you click on the parent button, you can see that the parent-child scope changes simultaneously

Then when you click on the Child button, the value of the sub-scope is updated

Then when I click on the parent button again, I find that the parental scope is updated, but the child scope is not changing.

This is because the inheritance between parent-child scopes is achieved by a string number or Boolean value , which is the value of JS copy .

Then run the following code:

Html

<div ng-controller= "Somecontroller" > {{somemodel.somevalue}} <button ng-click= "someaction ()" > Parent button</button> <div ng-controller= "Childcontroller" > {{somemodel.somevalue}} <but Ton ng-click= "Childaction ()" >child button</button> </div></div>

Js

Angular.module (' myApp ', []). Controller (function($scope) { $scope. Somemodel ='    Hello computer 'function= ' Hello Human, from parent ';  };    }). controller (functionfunction= ' Hello Human, from child ';  }; });

You can see that the difference between this example and the example above is that the child scope inherits from the parent scope, which is implemented by an object Somemodel, and runs the example.

After the initial completion, the parent-child scope displays the same string.

The parent button or child button then displays the same string synchronously, regardless of whether they are clicked.

This is because the inheritance between parent-child scopes is implemented by an object , which is the reference copy of JS .

In addition, in addition to Ng-controller

Ng-include

Ng-switch

Ng-repeat

Ng-view

Ng-if

All have the same characteristics.

Ng-controller value replication and reference replication in Angular JS

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.