search
HomeWeb Front-endJS TutorialAngularJS Documentation Reading Scope Directive How much do you know? Detailed explanation of scope directive in angularjs document reading

This article mainly introduces the scope directive for angularjs document reading. This article explains in detail the complete use of the scope directive for angularjs document reading. Let’s take a look at this article now

scope

directive is the most popular one in AngularJS Commonly used functions make it easy for us to implement code reuse in the frontend. The essence of the instruction lies in the interaction between the inner and outer domains of the instruction scope.

This article is a translation of a document plus some of my own understanding of it. Due to my limited level, there may be some places where the translation is not smooth or the translation is wrong. You are welcome to criticize and correct me. The usage and description of scope in this article are translated from the AngularJS English document. Document address: AngularJS official document

scope The value of the attribute can be false, which can be true or an object.

false

false: This is the default attribute of the directive scope. A scope will not be created for the directive. This The directive will use its parent scope.

true

true: Creates a child scope for the directive that prototypically inherits from the parent scope.

Object

{key: value}: Creates a new isolation scope for the directive, isolation scope and usually The difference between scope is: Isolated scope does not do prototypal inheritance from the parent scope.

Does not do prototypal inheritance from the parent scope. This is useful for creating reusable components. Reusable components should not read or modify properties from the parent scope.

Note: A directive with isolate scope but without template or templateUrl will not isolate scopeApply to its child elements. This is written in the document, but I still don’t understand what it means.

Maybe my translation is wrong, the following is the original text:

Note that an isolate scope directive without a template or templateUrl will not apply the isolate scope to its children elements.

The isolation object defines a local scope attribute collection originating from the attributes of the directive element.

scope binding

The following bindings can all add parameters.

Example:

scope: {
    name: '=nameAttr'
}

is bound to: <test name-attr="'hello'"></test>.

scope: {
    name: '='
}

is bound to: <test name="'hello'"></test>.

String binding

@/@attr: Bind local scope attributes to DOM The value of the attribute, this result is always a string, because the DOM attribute is a string. As the DOM property value changes, the property on the directive scope will also change, because this property is read on its parent scope.

Two-way binding

=/=attr: attributes of the local scope and expressions passed to the attributes To establish a two-way binding, the expression is evaluated within the scope of the parent scope. If the bound expression is not assignable, or it is not optional but is not passed in the directive, a $compile:noassign exception will be thrown because it cannot be combined with the parent scopeSynchronize.

By default, the $watch method is usually used to monitor changes and perform equality judgments based on the address of the object. However, if an object address or array address is passed into a bound expression, the comparison is by checking whether the values ​​are equal. You can also use =*/=*attr and $watchCollection for shallow monitoring.

I still don’t quite understand this passage. I found a reliable answer in StackOverflow, but I still don’t quite understand it. AngularJS =* Problem

(If you want to see more, go to the PHP Chinese websiteangularjs learning manual to learn)

One-way binding

/<code><attr>: Establishes a one-way binding between the local <code>scope and the expression passed to the DOM attribute, all Changes in the expression on the DOM attribute will be reflected on the scope attribute, but changes on the scope attribute will not be reflected on the DOMOn the expression of the attribute.

But there are two warnings:

1. One-way binding does not copy the value of the parent scope to the isolated scope , but simply set the same value. If you pass an object, changes to the object on the isolated scope will be reflected on the parent scope, because both reference the same object.

2.单向绑定监视的是父值地址的改变。这意味着在父值上的$watch仅仅在引用的地址发生改变时才会生效。大多数情况下,这是不需要关心的。但是必须要知道如果你单向绑定了一个对象,然后会改变隔离scope上的对象,如果改变了父scope上的该对象的一个属性,这个改变是不会传递到隔离scope上的,因为这个对象的地址没有改变,除非你赋值一个新的对象。

如果不打算将隔离scope的改变传播会父节点,单向绑定是很有用的。

绑定方法

&/&attr:在父scope提供一个可执行的表达式,就是传一个方法。

设置可选

所有的绑定(@, =, )都能通过在表达式上添加<code>?设置为可选的,这个标志必须在绑定模式之后,属性名称之前。

可选和不可选的区别在于:

  • 绑定是可选的,这个属性不会被定义。

  • 绑定不是可选的,这个属性被定义了。

以下是AngularJS文档中对可选指令的示例代码。

app.directive('testDir', function() {
  return {
    scope: {
      notoptional: '=',
      optional: '=?',
    },
    bindToController: true,
    controller: function() {
      this.$onInit = function() {
        console.log(this.hasOwnProperty('notoptional')); // true
        console.log(this.hasOwnProperty('optional')); // false
      }
    }
  };
});

本篇文章到这就结束了(想看更多就到PHP中文网angularjs学习手册中学习),有问题的可以在下方留言提问

The above is the detailed content of AngularJS Documentation Reading Scope Directive How much do you know? Detailed explanation of scope directive in angularjs document reading. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment