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指令會創(chuàng)建一個新的作用域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元素相關(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án)中的每個dom元素新建一個作用域。通過F12調(diào)試,可以看到scope0和scope1的內(nèi)容如下:


可以看到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>
當(dāng)點(diǎn)擊method1、method2、method3的時候,我們希望將按鈕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中某個屬性的值,那么該屬性一定不能是javascript基本數(shù)據(jù)類型,一定要是對象類型。而且不能直接通過=進(jìn)行賦值修改,必須是調(diào)用對象的方法來修改。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計有所幫助。
相關(guān)文章
使用Angular CLI進(jìn)行單元測試和E2E測試的方法
這篇文章主要介紹了使用Angular CLI進(jìn)行單元測試和E2E測試的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
angular2 ng build部署后base文件路徑問題詳細(xì)解答
本篇文章主要介紹了angular2 ng build部署后base文件路徑問題詳細(xì)解答,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
用Angular實(shí)現(xiàn)一個掃雷的游戲示例
這篇文章主要介紹了用Angular實(shí)現(xiàn)一個掃雷的游戲示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解
本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
angularjs 指令實(shí)現(xiàn)自定義滾動條效果
橫向商品欄,把原有的滾動條改成自定義的樣式,并且給兩邊加上箭頭可以調(diào)整,可以拖動商品和滾輪實(shí)現(xiàn)滾動條效果,這篇文章主要介紹了angularjs 指令實(shí)現(xiàn)自定義滾動條效果,需要的朋友可以參考下2024-03-03

