Description and usage of modifiers when direve ve declares a scope object in angularjs

Source: Internet
Author: User

Description and usage of modifiers when direve ve declares a scope object in angularjs

When we define the directive method in angular, we can see that

return {    restrict: 'AE',    scope: {},    template: '',    link: function() {}}

In addition to the attributes in the Code, some other attributes are available for configuration, which are not described here.

Today, we will focus on the scope field.


General usage settings

scope: {    name: '=',    age: '=',    sex: '@',    say: '&'}

Suppose our hml code is as follows:

 

Part of the corresponding controller code

Function Controller ($ scope) {$ scope. name = 'pajjket '; $ scope. age = 99; $ scope. sex = 'I am mal'; $ scope. say = function () {alert ('Hello, I am a pop-up message ');};}


So what are the meanings of these modifiers? How are they associated?

"=": The attribute value in the instruction is the value corresponding to the attribute value on $ scope in controller, which can be used for bidirectional data binding.

"@": The value in the command is the literal/Direct Volume in html. A local scope property is bound to the DOM property. Because the attribute value is always of the String type, this value always returns a String. If the attribute name is not specified through @ attr, the local name will be consistent with the DOM attribute name. For example The scope of the widget is defined as: {localName: '@ myAttr '}. The localName of the widget scope property maps the "hello {name}" converted value. After the name attribute value changes, the localName attribute of the widget scope also changes accordingly (only one-way, different from "=" below ). The name attribute is read in the parent scope (not the component scope)

"&": The value in the instruction is the attribute corresponding to $ scope in Contoller, but this attribute must be a function callback.


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.