詳細(xì)談?wù)凙ngularJS的子級作用域問題
前言
AngularJS自帶指令目前有ng-include
、ng-view
、ng-switch
、ng-repeat
。這樣的原因是因為,這些指令雖然是AngularJS內(nèi)部定義的,但是也是和directive
實現(xiàn)的方法都是一樣的,其內(nèi)部使用的是scope:true
的方式,子作用域繼承了父級的作用,并且構(gòu)建了一個獨立的子作用域,所有雙向綁定實現(xiàn)不了,只能單獨實現(xiàn)子級作用域繼承父級的屬性。
AngularJS的繼承是通過javascript的原型繼承方式實現(xiàn)的,進(jìn)行原型繼承即意味著父作用域在子作用域的原型鏈上。因為原型鏈的檢索只會在屬性檢索的時候觸發(fā),不會在改變屬性值的時候觸發(fā)。所以我們需要把原始類型轉(zhuǎn)換成對象,把值綁定在對象的屬性上。
大家可以在示例上看到,經(jīng)過改造之后,就可以實現(xiàn)子級修改父級作用域的屬性。原始類型只能繼承父類的作用域。
實現(xiàn)方法目前看有三種,下面一次來介紹
通過給父級scope上添加{}來實現(xiàn),把原始類型轉(zhuǎn)換成對象。
代碼如下:
<!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>父級作用域</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語法來實現(xiàn)
controller as
其實相當(dāng)于controller
的示例對象,原理還是把原始類型轉(zhuǎn)換成對象類型。
<!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>父級作用域</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)部方法來實現(xiàn)。
進(jìn)行原型繼承即意味著父作用域在子作用域的原型鏈上,這是JavaScript的特性。
AngularJS的作用域還存在如下內(nèi)部定義的關(guān)系:
scope.$parent指向scope的父作用域;
scope.$$childHead指向scope的第一個子作用域;
scope.$$childTail指向scope的最后一個子作用域;
scope.$$nextSibling指向scope的下一個相鄰作用域;
scope.$$prevSibling指向scope的上一個相鄰作用域;
通過在子級作用域中使用scope.$parent.name
,來獲取對父級作用域的雙向綁定。
示例如下:
<!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>父級作用域</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子級作用域問題的全部內(nèi)容,希望對大家學(xué)習(xí)和工作能有所幫助。大家如果有什么疑問,歡迎提出來。
- Angularjs全局變量被作用域監(jiān)聽的正確姿勢
- 關(guān)于angularJs指令的Scope(作用域)介紹
- 淺談angularJS 作用域
- 詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
- 詳解AngularJS中的作用域
- AngularJS實現(xiàn)單獨作用域內(nèi)的數(shù)據(jù)操作
- AngularJS入門教程之Scope(作用域)
- AngularJS Controller作用域
- 詳解angularjs中的隔離作用域理解以及綁定策略
- 詳談AngularJs 控制器、數(shù)據(jù)綁定、作用域
- AngularJS中的作用域?qū)嵗治?/a>
相關(guān)文章
ui-router中使用ocLazyLoad和resolve的具體方法
這篇文章主要介紹了ui-router中使用ocLazyLoad和resolve的具體方法,詳細(xì)的介紹了ocLazyLoad和resolve的具體用法,非常具有實用價值,需要的朋友可以參考下2017-10-10angular2+node.js express打包部署的實戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07AngularJS模塊學(xué)習(xí)之Anchor Scroll
這篇文章主要介紹了AngularJS模塊學(xué)習(xí)之Anchor Scroll 的相關(guān)資料,需要的朋友可以參考下2016-01-01詳解Angular的雙向數(shù)據(jù)綁定(MV-VM)
本文主要對Angular的雙向數(shù)據(jù)綁定(MV-VM)進(jìn)行實例分析,具有一定的參考價值,下面跟著小編一起來看下吧2016-12-12在AngularJS中使用jQuery的zTree插件的方法
這篇文章主要介紹了在AngularJS中使用jQuery的zTree插件的方法,Angular中集成了jqLite,但還不是完全版的jQuery,需要的朋友可以參考下2016-04-04