Table of Contents
scope
false
true
Object
scope binding
String binding
Two-way binding
One-way binding
绑定方法
设置可选
Home Web Front-end JS Tutorial AngularJS document reading directive scope

AngularJS document reading directive scope

Jul 09, 2018 pm 03:58 PM
angular.js directive

<p>This article mainly introduces the scope command for reading AngularJS documents. It has certain reference value. Now I share it with you. Friends in need can refer to the </p> <h1 id="scope">scope</h1> <p> command. It is the most commonly used function in <code>AngularJS</code>, allowing us to easily implement code reuse in the frontend. The essence of the instruction lies in the interaction between the inner and outer domains of the instruction <code>scope</code>. </p> <p>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 <code>scope</code> in this article are translated from the <code>AngularJS</code> English document. Document address: AngularJS official document </p> <p><code>scope</code> The value of the attribute can be <code>false</code>, which can be <code>true</code> or an object. </p> <h2 id="false">false</h2> <p><code>false</code>: This is the default attribute of the directive <code>scope</code>. A <code>scope</code> will not be created for the directive. This The directive will use its parent <code>scope</code>. </p> <h2 id="true">true</h2> <p><code>true</code>: Creates a child <code>scope</code> for the directive that prototypically inherits from the parent <code>scope</code>. </p> <h2 id="Object">Object</h2> <p><code>{key: value}</code>: Creates a new isolation <code>scope</code> for the directive, isolation <code>scope</code> and usually The difference between <code>scope</code> is: Isolated <code>scope</code> does not do prototypal inheritance from the parent <code>scope</code>. </p> <p> Does not do prototypal inheritance from the parent <code>scope</code>. This is useful for creating reusable components. Reusable components should not read or modify properties from the parent <code>scope</code>. </p> <p><strong>Note: A directive with isolate <code>scope</code> but without <code>template</code> or <code>templateUrl</code> will not isolate <code>scope</code>Apply to its child elements. </strong>This is written in the document, but I still don’t understand what it means. </p> <p>Maybe my translation is wrong, the following is the original text: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">Note that an isolate scope directive without a template or templateUrl will not apply the isolate scope to its children elements.</pre><div class="contentsignin">Copy after login</div></div> <p>The isolation object defines a local <code>scope</code> attribute collection originating from the attributes of the directive element. </p> <h3 id="scope-binding">scope binding</h3> <p>The following bindings can all add parameters. </p> <p>Example: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">scope: {     name: '=nameAttr' }</pre><div class="contentsignin">Copy after login</div></div> <p> is bound to: <code><test name-attr="'hello'"></test></code>. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">scope: {     name: '=' }</pre><div class="contentsignin">Copy after login</div></div> <p> is bound to: <code><test name="'hello'"></test></code>. </p> <h3 id="String-binding">String binding</h3> <p><code>@</code>/<code>@attr</code>: Bind local <code>scope</code> attributes to <code>DOM The value of the </code> attribute, this result is always a string, because the <code>DOM</code> attribute is a string. As the <code>DOM</code> property value changes, the property on the directive <code>scope</code> will also change, because this property is read on its parent <code>scope</code>. </p> <h3 id="Two-way-binding">Two-way binding</h3> <p><code>=</code>/<code>=attr</code>: attributes of the local <code>scope</code> and expressions passed to the attributes To establish a two-way binding, the expression is evaluated within the scope of the parent <code>scope</code>. If the bound expression is not assignable, or it is not optional but is not passed in the directive, a <code>$compile:noassign</code> exception will be thrown because it cannot be combined with the parent <code>scope</code>Synchronize. </p> <p>By default, the <code>$watch</code> 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 <code>=*</code>/<code>=*attr</code> and <code>$watchCollection</code> for shallow monitoring. </p> <p> I still don’t quite understand this passage. I found a reliable answer in <code>StackOverflow</code>, but I still don’t quite understand it. AngularJS =* Problem</p> <h3 id="One-way-binding">One-way binding</h3> <p><code><</code>/<code><attr</code>: In local <code>scope</code> and pass A one-way binding is established between the expressions on the <code>DOM</code> attribute. All changes in the expression on the <code>DOM</code> attribute will be reflected on the <code>scope</code> attribute. But changes to the <code>scope</code> attribute will not be reflected in the expression of the <code>DOM</code> attribute. </p> <p><strong>But there are two warnings: </strong></p> <p>1. One-way binding does not copy the value of the parent <code>scope</code> to the isolated <code>scope </code>, but simply set the same value. If you pass an object, changes to the object on the isolated <code>scope</code> will be reflected on the parent <code>scope</code>, because both reference the same object. </p> <p>2. One-way binding monitors changes in the parent value address. This means that <code>$watch</code> on the parent value will only take effect if the referenced address changes. In most cases, this is nothing to worry about. But you must know that if you bind an object one-way, then the object on the isolation <code>scope</code> will be changed. If you change an attribute of the object on the parent <code>scope</code>, this change It will not be passed to the isolation <code>scope</code> because the address of this object has not changed unless you assign a new object. </p> <p>One-way binding is useful if you do not intend to propagate changes to the isolated <code>scope</code> to the parent node. </p> <h3 id="绑定方法">绑定方法</h3> <p><code>&</code>/<code>&attr</code>:在父<code>scope</code>提供一个可执行的表达式,就是传一个方法。</p> <h3 id="设置可选">设置可选</h3> <p>所有的绑定(<code>@, =, <, &</code>)都能通过在表达式上添加<code>?</code>设置为可选的,这个标志必须在绑定模式之后,属性名称之前。</p> <p>可选和不可选的区别在于:</p> <ul class=" list-paddingleft-2"> <li><p>绑定是可选的,这个属性不会被定义。</p></li> <li><p>绑定不是可选的,这个属性被定义了。</p></li> </ul> <p>以下是<code>AngularJS</code>文档中对可选指令的示例代码。</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">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       }     }   }; });</pre><div class="contentsignin">Copy after login</div></div> <p>以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!</p> <p>相关推荐:</p> <p class="comments-box-content"><a title="AngularJS 表格导出添加额外信息" href="http://www.php.cn/js-tutorial-406294.html" target="_blank">AngularJS 表格导出添加额外信息</a><br></p> <p class="mt20 ad-detail-mm hidden-xs"><a title="angularjs的数据绑定" href="http://www.php.cn/js-tutorial-406293.html" target="_blank">angularjs的数据绑定</a><br></p>

The above is the detailed content of AngularJS document reading directive scope. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Angular learning talks about standalone components (Standalone Component) Angular learning talks about standalone components (Standalone Component) Dec 19, 2022 pm 07:24 PM

This article will take you to continue learning angular and briefly understand the standalone component (Standalone Component) in Angular. I hope it will be helpful to you!

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

What should I do if the project is too big? How to split Angular projects reasonably? What should I do if the project is too big? How to split Angular projects reasonably? Jul 26, 2022 pm 07:18 PM

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!

Let's talk about how to customize the angular-datetime-picker format Let's talk about how to customize the angular-datetime-picker format Sep 08, 2022 pm 08:29 PM

How to customize the angular-datetime-picker format? The following article talks about how to customize the format. I hope it will be helpful to everyone!

How to solve '[Vue warn]: Failed to resolve directive' error How to solve '[Vue warn]: Failed to resolve directive' error Aug 20, 2023 pm 05:54 PM

How to Fix "[Vuewarn]:FailedtoresolveDirective" Error Vue.js is a popular JavaScript framework that provides many useful features to develop interactive web applications. One such feature is a directive, which can be used to extend the functionality of an HTML element or add specific behavior. However, sometimes you may encounter an error when using the directive: "[Vuewarn]:F

A step-by-step guide to understanding dependency injection in Angular A step-by-step guide to understanding dependency injection in Angular Dec 02, 2022 pm 09:14 PM

This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

In-depth understanding of NgModule (module) in Angular In-depth understanding of NgModule (module) in Angular Sep 05, 2022 pm 07:07 PM

The NgModule module is an important point in Angular, because the basic building block of Angular is NgModule. This article will take you through the NgModule module in Angular. I hope it will be helpful to you!

See all articles