本文實例分析了AngularJS的ng-repeat指令與scope繼承關系。分享給大家供大家參考,具體如下:
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環境下,必須通過{{button}}這種方式取值。ng-repeat指令中$index代表遍歷的數組的索引,從0開始。
我們知道ng-controller指令會創建一個新的作用域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()方法來獲得某一個DOM元素相關聯的作用域。
<!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元素上關聯的作用域
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指令會新建作用域,而且是為循環中的每個dom元素新建一個作用域。通過F12調試,可以看到scope0和scope1的內容如下:


可以看到scope0和scope1中都有一個buttons屬性,這個屬性就是從父作用域下繼承得到的,很類似于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>
當點擊method1、method2、method3的時候,我們希望將按鈕button1、button2、button3替換掉。運行上面的代碼可以發現:method2和method3都能成功達到目的,但是method1不能達到目的。這其實很類似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中某個屬性的值,那么該屬性一定不能是javascript基本數據類型,一定要是對象類型。而且不能直接通過=進行賦值修改,必須是調用對象的方法來修改。
更多關于AngularJS相關內容感興趣的讀者可查看本站專題:《AngularJS入門與進階教程》及《AngularJS MVC架構總結》
希望本文所述對大家AngularJS程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。