用法:ng-repeat="extension";

extension(表达式) 定义了如何循环集合。

表达式实例规则:

1. x in records

2. (key,value) in myObj

3. x in records track by $id(x)

我们可以使用ng-repeat指令遍历一个JavaScript数组,当数组中有重复元素的时候,AngularJS会报错:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in users, Duplicate key: number:1。

如下面的代码就会报错:

<!DOCTYPE html>
<html ng-app="app" ng-controller="appCtrl">
<head>
<title>Self Document</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
var app = angular.module('app',[]);
app.controller('appCtrl',function($scope,$element){
$scope.dataList = [1,2,1];
});
</head>
<body>
<div ng-repeat="value in dataList">
<p>
<span ng-bind="value"></span>
</p>
</div>
</body>
</html>

这是因为ng-repeat不允许集合中存在两个相同id的对象。

For example: item in items is equivalent to item in items track by $id(item). This implies that the DOM elements will be associated by item identity in the array.

对于数字或者字符串等基本数据类型来说,它的id就是它自身的值。因此数组中是不允许存在两个相同的数字的。为了规避这个错误,需要定义自己的track by表达式。

// 业务上自己生成唯一的id
1. item in items track by item.id

//或者直接拿循环的索引变量$index来用
2. item in items track by $index

另外:如果集合是Javascript对象类型数据,那么就算值一摸一样,ng-repeat也不会认为这是相同的对象。

如果将上面的代码中dataList,那么是不会报错的。比如$scope.dataList = [{"num":10},{"num":10}];

参考文章:http://blog.csdn.net/aitangyong/article/details/44100921

AngularJS ng-repeat使用及注意事项的更多相关文章

  1. Part 6 AngularJS ng repeat directive

    ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...

  2. AngularJs压缩时须要注意的事项

    因为AngularJS是通过控制器构造函数的參数名字来判断依赖服务名称的.所以假设你要压缩控制器的JS代码.它全部的參数也同一时候会被压缩,这时候依赖注入系统就不能正确的识别出服务了. 假如我们的Co ...

  3. part 4 AngularJS ng src directive

  4. Part 15 AngularJS ng init directive

    The ng-init directive allows you to evaluate an expression in the current scope.  In the following e ...

  5. table sorting–angularjs

    1: <script type="text/javascript" ng:autobind 2: src="http://code.angularjs.org/0. ...

  6. [译]用AngularJS构建大型ASP.NET单页应用(三)

    原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single A ...

  7. [AngularJS] AngularJS系列(1) 基础篇

    目录 什么是AngularJS? 为什么使用/ng特性 Hello World 内置指令 内置过滤器 模块化开发 一年前开始使用AngularJS(以后简称ng),如今ng已经出2了.虽说2已完全变样 ...

  8. 从angularJS看MVVM

    javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到angularJS[ng]这么重量级的 ...

  9. angularJS看MVVM

    从angularJS看MVVM   javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到a ...

  10. js架构设计模式——从angularJS看MVVM

    javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到 angularJS[ng] 这么重量 ...

随机推荐

  1. php mysqli MySQL server has gone away 问题分析

    结论 mysqli没有处理对端(MySQL server)的断开请求.在send时,收到对端(MySQL server)的RST,会打印MySQL server has gone away. 环境 x ...

  2. JF厂V8版本爱彼AP15703,黄家橡树离岸型,超越N厂神器

    根据调查的结果JF厂的爱彼AP15703几乎常年垄断了爱彼的市场,销量持续性的排在爱彼整个品牌中的第一位.JF厂这两年一直在攻克爱彼整个品牌,有了解的都知道 爱彼15703以前是N厂的五大复刻神器的代 ...

  3. Spring框架学习之高级依赖关系配置(一)

    上篇文章我们对Spring做了初步的学习,了解了基本的依赖注入思想.学会简单的配置bean.能够使用Spring容器管理我们的bean实例等.但这还只是相对较浅显的内容,本篇将介绍bean的相关更高级 ...

  4. 如何阻止sql注入(pdo篇)

    Use prepared statements and parameterized queries. These are SQL statements that are sent to and par ...

  5. Leetcode题解(十八)

    51.N-Queens ---------------------------------------------------------------------------------分割线---- ...

  6. C# linq左连接与分组

    1.左连接使用DefaultIfEmpty(): 2.分组时候判断newper.FirstOrDefault() == null ? null: newper.ToList()这个经常出错误,如果不判 ...

  7. css元素选择器 first-child nth-child

    E:first-child   只要E元素是它的父级的第一个子元素,就选中.它需要同时满足两个条件,    (1)是"第一个子元素",    (2)是"这个子元素刚好是E ...

  8. struts2框架的登录制作

    首先:我们要建一个web项目 接着: 我们先来导入struts的xml文件 第一步:右击你的项目名,鼠标到MyEclipse会看到一个add struts开头的文件,点开以后看到: 这里我们选择str ...

  9. 2017阿里云双11-云服务器ECS优惠活动最强解读和购买指南

    本站之前介绍了<爆款云服务器,限时2折起>,这其实是阿里云双11之前的预热活动:四款低配的机型,二折给用户(每个用户限购一台),非常的实惠,有很多阅读了本站文章的用户都一次性购买了三年的. ...

  10. Mybatis按顺序获取数据

    sql语句select * from producttg where hospitalcode in (1,2,3)  获取到的数据并不是按照条件1,2,3的顺序排列,如果要成下面形式(mybatis ...