詳細(xì)談?wù)凙ngularJS的子級作用域問題
前言
AngularJS自帶指令目前有ng-include、ng-view、ng-switch、ng-repeat。這樣的原因是因?yàn)椋@些指令雖然是AngularJS內(nèi)部定義的,但是也是和directive實(shí)現(xiàn)的方法都是一樣的,其內(nèi)部使用的是scope:true的方式,子作用域繼承了父級的作用,并且構(gòu)建了一個(gè)獨(dú)立的子作用域,所有雙向綁定實(shí)現(xiàn)不了,只能單獨(dú)實(shí)現(xiàn)子級作用域繼承父級的屬性。
AngularJS的繼承是通過javascript的原型繼承方式實(shí)現(xiàn)的,進(jìn)行原型繼承即意味著父作用域在子作用域的原型鏈上。因?yàn)樵玩湹臋z索只會在屬性檢索的時(shí)候觸發(fā),不會在改變屬性值的時(shí)候觸發(fā)。所以我們需要把原始類型轉(zhuǎn)換成對象,把值綁定在對象的屬性上。

大家可以在示例上看到,經(jīng)過改造之后,就可以實(shí)現(xiàn)子級修改父級作用域的屬性。原始類型只能繼承父類的作用域。
實(shí)現(xiàn)方法目前看有三種,下面一次來介紹
通過給父級scope上添加{}來實(shí)現(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語法來實(shí)現(xiàn)
controller as其實(shí)相當(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)部方法來實(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è)相鄰作用域;
通過在子級作用域中使用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實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作
- AngularJS入門教程之Scope(作用域)
- AngularJS Controller作用域
- 詳解angularjs中的隔離作用域理解以及綁定策略
- 詳談AngularJs 控制器、數(shù)據(jù)綁定、作用域
- AngularJS中的作用域?qū)嵗治?/a>
相關(guān)文章
利用Angularjs中模塊ui-route管理狀態(tài)的方法
這篇文章主要給大家介紹了如何用Angularjs中的模板ui-route來管理狀態(tài)的方法,文中通過示例代碼介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友可以一起來學(xué)習(xí)學(xué)習(xí)。2016-12-12
詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù)
這篇文章主要介紹了詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù),詳細(xì)的介紹了ui-sref和$state.go的區(qū)別和如何傳參,有興趣的可以了解下2017-04-04
Angular組件庫ng-zorro-antd實(shí)現(xiàn)radio單選框選擇
這篇文章主要為大家介紹了Angular組件庫ng-zorro-antd實(shí)現(xiàn)radio單選框取消選擇實(shí)現(xiàn)問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
AngularJS動態(tài)添加數(shù)據(jù)并刪除的實(shí)例
下面小編就為大家分享一篇AngularJS動態(tài)添加數(shù)據(jù)并刪除的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
深入學(xué)習(xí)AngularJS中數(shù)據(jù)的雙向綁定機(jī)制
這篇文章主要介紹了AngularJS中數(shù)據(jù)的雙向綁定機(jī)制,雙向綁定使得HTML中呈現(xiàn)的view與AngularJS中的數(shù)據(jù)一致,是Angular的重要特性之一,需要的朋友可以參考下2016-03-03
Angular中的ActivatedRoute和Router原理解釋
這篇文章主要為大家介紹了Angular中的ActivatedRoute和Router原理解釋,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

