詳細(xì)談?wù)凙ngularJS的子級(jí)作用域問題
前言
AngularJS自帶指令目前有ng-include
、ng-view
、ng-switch
、ng-repeat
。這樣的原因是因?yàn)?,這些指令雖然是AngularJS內(nèi)部定義的,但是也是和directive
實(shí)現(xiàn)的方法都是一樣的,其內(nèi)部使用的是scope:true
的方式,子作用域繼承了父級(jí)的作用,并且構(gòu)建了一個(gè)獨(dú)立的子作用域,所有雙向綁定實(shí)現(xiàn)不了,只能單獨(dú)實(shí)現(xiàn)子級(jí)作用域繼承父級(jí)的屬性。
AngularJS的繼承是通過javascript的原型繼承方式實(shí)現(xiàn)的,進(jìn)行原型繼承即意味著父作用域在子作用域的原型鏈上。因?yàn)樵玩湹臋z索只會(huì)在屬性檢索的時(shí)候觸發(fā),不會(huì)在改變屬性值的時(shí)候觸發(fā)。所以我們需要把原始類型轉(zhuǎn)換成對(duì)象,把值綁定在對(duì)象的屬性上。
大家可以在示例上看到,經(jīng)過改造之后,就可以實(shí)現(xiàn)子級(jí)修改父級(jí)作用域的屬性。原始類型只能繼承父類的作用域。
實(shí)現(xiàn)方法目前看有三種,下面一次來介紹
通過給父級(jí)scope上添加{}來實(shí)現(xiàn),把原始類型轉(zhuǎn)換成對(duì)象。
代碼如下:
<!DOCTYPE html> <html lang="en" ng-app="childScope"> <head> <meta charset="UTF-8"> <title></title> <script src="lib/angular.min.js" type="text/javascript"></script> <style> .inputOne{ width: 100px; height: 50px; background: skyblue; } .inner{ border: 1px solid skyblue; width: 200px; height: 150px; } .outer{ border: 1px solid salmon; width: 200px; height: 150px; } .sco{ background: skyblue; } </style> </head> <body ng-controller="childCon"> <div class="inner"> <h3>父級(jí)作用域</h3> <span>{{vm.private1}}</span> <span>{{vm.private2}}</span> </div> <div class="outer"> <h3>自己作用域</h3> <div class="one" ng-include src="'one.html'"></div> <div class="two" ng-include src="'two.html'"></div> </div> </body> <script> var app=angular.module("childScope",['template']) .controller("childCon",["$scope", function ($scope) { var vm=$scope.vm={}; vm.private1=12; vm.private2=13; $scope.test=123; }]); var template=angular.module("template",[]) .run(["$templateCache", function ($templateCache) { $templateCache.put("one.html","" + "<div><input type='text' ng-model='vm.private1'/></div>") }]) .run(["$templateCache", function ($templateCache) { $templateCache.put("two.html","" + "<div><input type='text' ng-model='vm.private2'/>" + "<div class='sco'><span>原始類型</span>{{test}}</div>" + "</div>") }]) </script> </html>
通過controller as語法來實(shí)現(xiàn)
controller as
其實(shí)相當(dāng)于controller
的示例對(duì)象,原理還是把原始類型轉(zhuǎn)換成對(duì)象類型。
<!DOCTYPE html> <html lang="en" ng-app="childScope"> <head> <meta charset="UTF-8"> <title></title> <script src="lib/angular.min.js" type="text/javascript"></script> <style> .inputOne{ width: 100px; height: 50px; background: skyblue; } .inner{ border: 1px solid skyblue; width: 200px; height: 150px; } .outer{ border: 1px solid salmon; width: 200px; height: 150px; } .sco{ background: skyblue; } </style> </head> <body ng-controller="childCon as vm"> <div class="inner"> <h3>父級(jí)作用域</h3> <span>{{vm.private1}}</span> <span>{{vm.private2}}</span> </div> <div class="outer"> <h3>自己作用域</h3> <div class="one" ng-include src="'one.html'"></div> <div class="two" ng-include src="'two.html'"></div> </div> </body> <script> var app=angular.module("childScope",['template']) .controller("childCon",["$scope", function ($scope) { this.private1=12; this.private2=22; $scope.test=123; }]); var template=angular.module("template",[]) .run(["$templateCache", function ($templateCache) { $templateCache.put("one.html","" + "<div><input type='text' ng-model='vm.private1'/></div>") }]) .run(["$templateCache", function ($templateCache) { $templateCache.put("two.html","" + "<div><input type='text' ng-model='vm.private2'/>" + "<div class='sco'><span>原始類型</span>{{test}}</div>" + "</div>") }]) </script> </html>
使用$parent.name調(diào)用內(nèi)部方法來實(shí)現(xiàn)。
進(jìn)行原型繼承即意味著父作用域在子作用域的原型鏈上,這是JavaScript的特性。
AngularJS的作用域還存在如下內(nèi)部定義的關(guān)系:
scope.$parent指向scope的父作用域;
scope.$$childHead指向scope的第一個(gè)子作用域;
scope.$$childTail指向scope的最后一個(gè)子作用域;
scope.$$nextSibling指向scope的下一個(gè)相鄰作用域;
scope.$$prevSibling指向scope的上一個(gè)相鄰作用域;
通過在子級(jí)作用域中使用scope.$parent.name
,來獲取對(duì)父級(jí)作用域的雙向綁定。
示例如下:
<!DOCTYPE html> <html lang="en" ng-app="childScope"> <head> <meta charset="UTF-8"> <title></title> <script src="lib/angular.min.js" type="text/javascript"></script> <style> .inputOne{ width: 100px; height: 50px; background: skyblue; } .inner{ border: 1px solid skyblue; width: 200px; height: 150px; } .outer{ border: 1px solid salmon; width: 200px; height: 150px; } .sco{ background: skyblue; } </style> </head> <body ng-controller="childCon"> <div class="inner"> <h3>父級(jí)作用域</h3> <span>{{private1}}</span> <span>{{private2}}</span> </div> <div class="outer"> <h3>自己作用域</h3> <div class="one" ng-include src="'one.html'"></div> <div class="two" ng-include src="'two.html'"></div> </div> </body> <script> var app=angular.module("childScope",['template']) .controller("childCon",["$scope", function ($scope) { $scope.private1=12; $scope.private2=22; $scope.test=123; }]); var template=angular.module("template",[]) .run(["$templateCache", function ($templateCache) { $templateCache.put("one.html","" + "<div><input type='text' ng-model='$parent.private1'/></div>") }]) .run(["$templateCache", function ($templateCache) { $templateCache.put("two.html","" + "<div><input type='text' ng-model='$parent.private2'/>" + "<div class='sco'><span>原始類型</span>{{test}}</div>" + "</div>") }]) </script> </html>
總結(jié)
以上就是AngularJS子級(jí)作用域問題的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)和工作能有所幫助。大家如果有什么疑問,歡迎提出來。
- Angularjs全局變量被作用域監(jiān)聽的正確姿勢(shì)
- 關(guān)于angularJs指令的Scope(作用域)介紹
- 淺談angularJS 作用域
- 詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
- 詳解AngularJS中的作用域
- AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作
- AngularJS入門教程之Scope(作用域)
- AngularJS Controller作用域
- 詳解angularjs中的隔離作用域理解以及綁定策略
- 詳談AngularJs 控制器、數(shù)據(jù)綁定、作用域
- AngularJS中的作用域?qū)嵗治?/a>
相關(guān)文章
AngularJS下對(duì)數(shù)組的對(duì)比分析
下面小編就為大家?guī)硪黄狝ngularJS下對(duì)數(shù)組的對(duì)比分析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08ui-router中使用ocLazyLoad和resolve的具體方法
這篇文章主要介紹了ui-router中使用ocLazyLoad和resolve的具體方法,詳細(xì)的介紹了ocLazyLoad和resolve的具體用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Angular之jwt令牌身份驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Angular之jwt令牌身份驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02angular2+node.js express打包部署的實(shí)戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07AngularJS模塊學(xué)習(xí)之Anchor Scroll
這篇文章主要介紹了AngularJS模塊學(xué)習(xí)之Anchor Scroll 的相關(guān)資料,需要的朋友可以參考下2016-01-01詳解Angular的雙向數(shù)據(jù)綁定(MV-VM)
本文主要對(duì)Angular的雙向數(shù)據(jù)綁定(MV-VM)進(jìn)行實(shí)例分析,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12在AngularJS中使用jQuery的zTree插件的方法
這篇文章主要介紹了在AngularJS中使用jQuery的zTree插件的方法,Angular中集成了jqLite,但還不是完全版的jQuery,需要的朋友可以參考下2016-04-04