search
HomeDatabaseMysql Tutorial【AngularJS系列2】scope

Scope是AngularJS里的一个很重要的概念,简单的说它就是用来保存AngularJS Model们的对象,是Model们温暖的小家~ 那这个小家是什么时候造的呢? 1 html ng-app = mainApp 2 / html 我们知道, ng-app 是一个应用启动AngularJS的入口点,在这里也会创建一个r

Scope是AngularJS里的一个很重要的概念,简单的说它就是用来保存AngularJS Model们的对象,是Model们温暖的小家~

那这个小家是什么时候造的呢?

<span>1</span> <span>html</span> <span>ng-app</span>=<span>"mainApp"</span>>
<span>2</span> <span><span>html</span>></span>

我们知道, ng-app 是一个应用启动AngularJS的入口点,在这里也会创建一个root scope,在controller里可以通过 $rootScope 调到,每个应用只能有一个root scope(当然了~root嘛~),但它会有多个child scope,那啥时候会创建child scope呢?

<span>1</span> <span>html</span> <span>ng-app</span>=<span>"mainApp"</span>>
<span>2</span>     <span>body</span> <span>ng-controller</span>=<span>"MainCtrl"</span>>
<span>3</span>         <span>div</span> <span>ng-controller</span>=<span>"SubCtrl"</span> <span>ng-include</span> <span>src</span>=<span>" 'template.html' "</span>>
<span>4</span>         <span><span>div</span>></span>
<span>5</span>         <span>ul</span>>
<span>6</span>             <span>li</span> <span>ng-repeat</span>=<span>"item in items"</span>>{{item.name}}<span><span>li</span>></span>
<span>7</span>         <span><span>ul</span>></span>
<span>8</span>     <span><span>body</span>></span>
<span>9</span> <span><span>html</span>></span>

在上面的例子里, ng-controller   ng-include   ng-repeat 都创建了新的child scope( ng-repeat 是对每一个重复的元素都创建了新的child scope),他们之间的父子关系是这样的:

【AngularJS系列2】scope

包含关系即是他们的父子关系,子scope是可以访问父scope上绑定的所有model和function的。

AngularJS会给scope对应的dom添加叫ng-scope的class,如果我们给自己的应用加这样一个css~

<span>1</span> <span><span>.ng-scope</span> </span><span>{<span><span><span>border</span></span></span><span>:<span><span> <span>2</span>px dotted red</span></span><span>;</span></span><span>}</span></span>

通过红色的虚线边框我们也可以看出来大概scope的范围,但注意,并不是所有的ng-scope都是新的scope,有些ng-scope类名对应的dom共享的是同一个scope。

细心的童鞋可能注意到, ng-controller="SubCtrl" 和 ng-include 放在同一个div上了,为啥 ng-controller="SubCtrl" 就是爸爸, ng-include 就是儿子呢?  
这个没啥特别的原因, ng-controller 在AngularJS底层代码里实现的比较靠前而已,与在div上标明的顺序无关,但是这时会发生一个问题:

假如在 ng-include 对应的 template.html 里有这样的代码:

template.html

<span>1</span> <span><span></span><span><span><span>input</span> </span><span><span>type</span></span><span>=<span>"text"</span></span><span> <span>ng-model</span></span><span>=<span>"lastName"</span></span> <span>></span></span></span>

我们会发现,在 ng-controller="SubCtrl" 这个controller里是取不到lastName 的值的。

原因是这样的~  
我们假设 ng-controller="SubCtrl" 对应的是 Scope A , ng-include 对应的是 Scope B ~

  1. ng-include 创建的 Scope B 是 ng-controller 创建的 Scope A 的子scope,所以在 template.html 里可以访问 Scope A 的model和function。
  2. 在 template.html 里用 ng-model 绑定的model,是存放在 Scope B 上的,Scope A 是拿不到的,即使model同名。

解决方案:

  • 直接对 Scope A 的model绑定成员对象,如 ng-model="user.lastName"
  • 或在 template.html 使用 ng-model 绑定model时,加上 $parent (取父scope),如: ng-model="$parent.lastName" ,这样info就绑定在 Scope A上了

比较推荐第一种方式,因为第一种抽象出了对象,比起第二种所有的model都直接绑在$scope上来,封装的更好~

这里是官方Scope介绍~

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
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version