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中訪(fǎng)問(wèn)button不需要使用{{button}}這種語(yǔ)法;而其他非AngularJS環(huán)境下,必須通過(guò){{button}}這種方式取值。ng-repeat指令中$index代表遍歷的數(shù)組的索引,從0開(kāi)始。
我們知道ng-controller指令會(huì)創(chuàng)建一個(gè)新的作用域scope,測(cè)試代碼如下:
<!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()方法來(lái)獲得某一個(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è)作用域。通過(guò)F12調(diào)試,可以看到scope0和scope1的內(nèi)容如下:
可以看到scope0和scope1中都有一個(gè)buttons屬性,這個(gè)屬性就是從父作用域下繼承得到的,很類(lèi)似于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í)很類(lèi)似C語(yǔ)言中傳值,還是傳引用的問(wèn)題。
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就類(lèi)似于我們上面的method1,而rightChangeName類(lèi)似于上面的method3。也就是說(shuō)如果我們想在childScope中修改parentScope中某個(gè)屬性的值,那么該屬性一定不能是javascript基本數(shù)據(jù)類(lèi)型,一定要是對(duì)象類(lèi)型。而且不能直接通過(guò)=進(jìn)行賦值修改,必須是調(diào)用對(duì)象的方法來(lái)修改。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《AngularJS入門(mén)與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用Angular CLI進(jìn)行單元測(cè)試和E2E測(cè)試的方法
這篇文章主要介紹了使用Angular CLI進(jìn)行單元測(cè)試和E2E測(cè)試的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03angular2 ng build部署后base文件路徑問(wèn)題詳細(xì)解答
本篇文章主要介紹了angular2 ng build部署后base文件路徑問(wèn)題詳細(xì)解答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07用Angular實(shí)現(xiàn)一個(gè)掃雷的游戲示例
這篇文章主要介紹了用Angular實(shí)現(xiàn)一個(gè)掃雷的游戲示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的步驟詳解
本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08angularjs 指令實(shí)現(xiàn)自定義滾動(dòng)條效果
橫向商品欄,把原有的滾動(dòng)條改成自定義的樣式,并且給兩邊加上箭頭可以調(diào)整,可以拖動(dòng)商品和滾輪實(shí)現(xiàn)滾動(dòng)條效果,這篇文章主要介紹了angularjs 指令實(shí)現(xiàn)自定義滾動(dòng)條效果,需要的朋友可以參考下2024-03-03詳解angular2.x創(chuàng)建項(xiàng)目入門(mén)指令
這篇文章主要介紹了詳解angular2.x創(chuàng)建項(xiàng)目入門(mén)指令,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10