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 in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use