Introduction to scope (scope) of ANGULARJS directives _angularjs

Source: Internet
Author: User

Whenever an instruction is created, there is a choice whether to inherit its own parent scope (generally the scope provided by the external controller or the root scope ($rootScope), or to create a new own scope, Of course Angularjs provides three choices for the scope parameters of our directives, respectively: false,true,{}; false by default.

1.scope = False

JS Code:

HTML code:

Result

Modifying the contents of the text box, two names will change, actually modifying the same $scope Name property.

2. Scope=true

Modify the above JS code, the instructions in the: Scope:false modified to Scope:true

Then we try to write a string in our input box and we find that the name in the instruction has changed, but the name outside the instruction has not changed, which illustrates a problem.

When we set the scope to true, we created a new scope, except that the scope was inherited from our parent scope, and I think it's understandable that our newly created scope is a new scope, only when initialized, Populate our new scope with the properties and methods of the parent scope. It is not the same scope as the parent scope.
When we set scope to false, the instruction that we create and the parent scope (in fact, the same scope) share the same model model, so the model data is modified in the instruction, and it is reflected in the model of the parent scope.

3. scope={}

When we set scope to {}, it means that we create a new scope that is isolated from the parent scope, which allows us to work properly without knowing the external environment, without relying on the external environment.

JS Code:

HTML code:

Result

Modifying the contents of a text box only the name in the instruction will be modified.

Scope: {@=&}

@
This is a prefix identifier for a single binding
How to: Use attributes in an element, such as <div my-directive my-name= "{{name}}" ></DIV> Note that the name of the property will be used-to connect two words, because it is a single binding of the data by using the {{ } to bind the data.

=
This is a two-way data binding prefix identifier
How to: Use attributes in an element, like this <div my-directive age= "age" ></div>, note that the two-way binding of the data is implemented through the = prefix identifier, so {{}} is not allowed.

&
This is the prefix identifier of a binding function method
How to: Use attributes in an element, such as <div my-directive change-my-age= "Changeage ()" ></div>, and note that the name of the attribute will be used-to connect multiple words.

The above is small series for everyone to bring about the scope of the ANGULARJS Directive (scope) to introduce the entire content, I hope that we support cloud-Habitat Community ~

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.