ng-repeat指令的使用方式可以参考如下代码:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>ng-repeat</title> <script src="jquery-1.8.3.js"></script> <script src="angular.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.8.3.js"></script> <script src="angular.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.8.3.js"></script> <script src="angular.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的$parent中都有一个buttons属性,这个属性就是从父作用域下继承得到的,很类似于javascript的原型链。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>ng-repeat</title> <script src="jquery-1.8.3.js"></script> <script src="angular.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不能达到目的。原因分析如下:
以于method2,它直接修改$parent的buttons,因此我们很容易看出它是可以达到替换的目的,调试查看$scope0如下:
而method1的scope0.buttons = ["a1","b1","c1"];方法,会给scope0增加buttons属性并赋值(相当于重新赋值,没有改变原型),并不会影响$parents的buttons的值,所以就不能替换,如下所示:
而对于methods方法中的scope0.buttons[0] = "a3";语句,首先会在当前作用域下找是否有buttons属性,发现没有,就会向父作用域(即JavaScript中原型链)查找,并找到了,然后修改,这里的修改实际上修改的就是$parents下的buttons,所以也生效了,如下所示:
原因同http://bijian1013.iteye.com/admin/blogs/2221083中所说的对于原型的写有copy-on-write特性是一样的。
文章来源:http://blog.csdn.net/aitangyong/article/details/44086137
相关推荐
本文实例分析了AngularJS的ng-repeat指令与scope继承关系。分享给大家供大家参考,具体如下: ng-repeat指令的使用方式可以参考如下代码: <!doctype html> <html lang=en> <head> <meta ...
本文实例讲述了AngularJS ng-repeat指令及Ajax的应用。分享给大家供大家参考,具体如下: ng-repeat 指令用于循环输出指定次数的 HTML 元素。集合必须是数组或对象。 定义: <element ng-repeat=expression> 说明...
angularjs,ng-repeat循环渲染时,无法获取dom对象
主要介绍了AngularJs ng-repeat 嵌套如何获取外层$index的相关资料,需要的朋友可以参考下
AngularJs 的 ng-repeat 让我们非常方便的遍历数组生成 Dom 元素,但是使用不当也会有性能问题。下面给大家分享在项目中使用ng-repeat指令实现下拉框。 1、问题背景 select下拉框里option组装成下拉框,这里利用ng-...
本文实例讲述了AngularJS ng-repeat指令中使用track by子语句解决重复数据遍历错误问题。分享给大家供大家参考,具体如下: 我们可以使用ng-repeat指令遍历一个JavaScript数组,当数组中有重复元素的时候,AngularJS...
NULL 博文链接:https://bijian1013.iteye.com/blog/2291632
本篇文章主要介绍了angularjs过滤器-filter与ng-repeat的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
本文主要讲解AngularJS ng-repeat 指令,这里对ng-repeat的基础资料做了整理,并附有示例代码,有兴趣的朋友可以参考下
ng-repeat 猫头鹰旋转木马在 AngularJS ng-repeat 中初始化 Owl Carousel 的指令#下载 ##NPM npm install ng-repeat-owl-carousel --save ##鲍尔bower install ng-repeat-owl-carousel --save #Usage 将源文件添加到...
ng-repeat-start与ng-repeat-end时AngularJS(1.2.x)扩展的,使用这两个指令可以灵活控制遍历形式。 例如: index.html <div class=uk-panel ng-controller=UserCtrl> <ul class=uk-list> <li ng-repeat-start=...
在ng-repeat中,含有动态的html,而这些html中含有自定义指令。 因为希望实现一个能够复用的table,所以定义了一个指令myStandTable,指令代码大概如下: var myCommon = angular.module("myCommon",[]); myCommon....
AngularJS ng-repeat遍历输出的用法,最近需要用,就顺便发到随笔上了 <!doctype html> <html> <head> <meta charset=utf-8> <title>ng-repeat directive</title> </head>...
主要介绍了AngularJS 获取ng-repeat动态生成的ng-model值实例详解的相关资料,这里提供实例代码及实现效果图,需要的朋友可以参考下