AngularJS的ng-repeat指令與scope繼承關(guān)系實(shí)例詳解
本文實(shí)例分析了AngularJS的ng-repeat指令與scope繼承關(guān)系。分享給大家供大家參考,具體如下:
ng-repeat指令的使用方式可以參考如下代碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ng-repeat</title>
<script src="jquery-1.11.1.js"></script>
<script src="angular-1.2.25.js"></script>
<script>
function wholeController($scope,$rootScope,$injector)
{
$scope.buttons = ["button1","button2","button3"];
$scope.btnFunc = function(value){
alert(value);
};
}
</script>
</head>
<body ng-app>
<div id="first" ng-controller="wholeController">
<div id="buttonDiv">
<input type="button" ng-repeat="button in buttons" id="btn{{$index}}" value="{{button}}" ng-click="btnFunc(button);"/>
</div>
<input type="button" value="test" ng-click="testFunc();">
</div>
</body>
</html>
這里需要注意:ng-click中訪問button不需要使用{{button}}這種語法;而其他非AngularJS環(huán)境下,必須通過{{button}}這種方式取值。ng-repeat指令中$index代表遍歷的數(shù)組的索引,從0開始。
我們知道ng-controller指令會(huì)創(chuàng)建一個(gè)新的作用域scope,測試代碼如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ng-repeat</title>
<script src="jquery-1.11.1.js"></script>
<script src="angular-1.2.25.js"></script>
<script>
//$scope是ng-controller指令新建的作用域
function wholeController($scope,$rootScope,$injector)
{
alert($scope.$parent === $rootScope);//輸出true
}
</script>
</head>
<body ng-app>
<div id="first" ng-controller="wholeController">
</div>
</body>
</html>
我們可以使用angular.element(domElement).scope()方法來獲得某一個(gè)DOM元素相關(guān)聯(lián)的作用域。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ng-repeat</title>
<script src="jquery-1.11.1.js"></script>
<script src="angular-1.2.25.js"></script>
<script>
function wholeController($scope,$rootScope,$injector)
{
$scope.buttons = ["button1","button2","button3"];
$scope.testFunc = function(){
//拿到dom元素上關(guān)聯(lián)的作用域
var scope0 = angular.element($("#btn0")[0]).scope();
var scope1 = angular.element($("#btn1")[0]).scope();
alert(scope0 == scope1);//輸出false
alert(scope0.$parent === $scope);//true
alert(scope1.$parent === $scope);//true
};
}
</script>
</head>
<body ng-app>
<div id="first" ng-controller="wholeController">
<div id="buttonDiv">
<input type="button" ng-repeat="button in buttons" id="btn{{$index}}" value="{{button}}" />
</div>
<input type="button" value="test" ng-click="testFunc();">
</div>
</body>
</html>
可以看到ng-repeat指令會(huì)新建作用域,而且是為循環(huán)中的每個(gè)dom元素新建一個(gè)作用域。通過F12調(diào)試,可以看到scope0和scope1的內(nèi)容如下:


可以看到scope0和scope1中都有一個(gè)buttons屬性,這個(gè)屬性就是從父作用域下繼承得到的,很類似于JavaScript的原型鏈。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ng-repeat</title>
<script src="jquery-1.11.1.js"></script>
<script src="angular-1.2.25.js"></script>
<script>
function wholeController($scope,$rootScope,$injector)
{
$scope.buttons = ["button1","button2","button3"];
$scope.method1 = function(){
var scope0 = angular.element($("#btn0")[0]).scope();
scope0.buttons = ["a1","b1","c1"];
};
$scope.method2 = function(){
var scope0 = angular.element($("#btn0")[0]).scope();
scope0.$parent.buttons = ["a2","b2","c2"];
};
$scope.method3 = function(){
var scope0 = angular.element($("#btn0")[0]).scope();
scope0.buttons[0] = "a3";
scope0.buttons[1] = "b3";
scope0.buttons[2] = "c3";
};
}
</script>
</head>
<body ng-app>
<div id="first" ng-controller="wholeController">
<div id="buttonDiv">
<input type="button" ng-repeat="button in buttons" id="btn{{$index}}" value="{{button}}" />
</div>
<input type="button" value="method1" ng-click="method1();">
<input type="button" value="method2" ng-click="method2();">
<input type="button" value="method3" ng-click="method3();">
</div>
</body>
</html>
當(dāng)點(diǎn)擊method1、method2、method3的時(shí)候,我們希望將按鈕button1、button2、button3替換掉。運(yùn)行上面的代碼可以發(fā)現(xiàn):method2和method3都能成功達(dá)到目的,但是method1不能達(dá)到目的。這其實(shí)很類似C語言中傳值,還是傳引用的問題。
var obj = {"name":"aty"};
wrongChangeName(obj);
alert(obj.name);//仍然是aty
rightChangeName(obj);
alert(obj.name);//hehe
function rightChangeName(obj)
{
obj.name="hehe";
}
function wrongChangeName(obj)
{
obj = {"name":"hehe"};
}
wrongChangeName就類似于我們上面的method1,而rightChangeName類似于上面的method3。也就是說如果我們想在childScope中修改parentScope中某個(gè)屬性的值,那么該屬性一定不能是javascript基本數(shù)據(jù)類型,一定要是對象類型。而且不能直接通過=進(jìn)行賦值修改,必須是調(diào)用對象的方法來修改。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
Angular異步執(zhí)行學(xué)習(xí)之zone.js使用
這篇文章主要為大家介紹了Angular異步執(zhí)行學(xué)習(xí)之zone.js使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Angular事件之不同組件間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了Angular事件之不同組件間傳遞數(shù)據(jù)的方法,利用Angular Event在不同組件之間傳遞數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
詳解Angular調(diào)試技巧之報(bào)錯(cuò)404(not found)
本篇文章主要介紹了詳解Angular調(diào)試技巧之報(bào)錯(cuò)404(not found),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
詳解Angular結(jié)合zTree異步加載節(jié)點(diǎn)數(shù)據(jù)
本篇文章主要給大家分享了Angular結(jié)合zTree異步加載節(jié)點(diǎn)數(shù)據(jù)的難點(diǎn)以及方法,有這方面需求的朋友參考下吧。2018-01-01

