AngularJs ng-repeat重复项异常解决方案
ng-repeat="v in arr track by $index"
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><title>Document</title></head><body ng-app="myApp"><!--ng-init:初始化数据ng-init="person={name:'小明'}"--><div ng-app="myApp" ng-controller="myController"><input type="text" ng-model="iptValue"><input type="button" ng-click="clickFn()" value="按钮"><ul><li ng-repeat="v in arr track by $index">{{v}}</li></ul></div></body><script type="text/javascript" src="angular.min.js"></script><script type="text/javascript">// [] ---> 依赖var app = angular.module("myApp",[]);// $scope作用域:作用域范围myControllerapp.controller("myController",function($scope){$scope.arr = ["111","222"];$scope.clickFn = function(){var v = $scope.iptValue;$scope.arr.push(v);$scope.iptValue = "";}});</script></html>

1.现象
- <ul ng-app="myApp" ng-controller="myCtrl">
- <li ng-repeat="x in items">
- <strong>{{x}}</strong>
- </li>
- </ul>
- <script>
- //使用ng-repeat 重复 html代码
- var app = angular.module('myApp', []);
- app.controller('myCtrl', function ($scope) {
- $scope.items = [1, 2, 3, 2];
- });
- </script>
当数组中有重复项时,抛出异常

点击异常链接,没有看到异常
2.解决方法
默认在ng-repeat的时候每一个item都要保证是唯一的,否则console就会打出error告诉你哪个key/value是重复的。
指定跟踪值唯一可以了
- <ul ng-app="myApp" ng-controller="myCtrl">
- <li ng-repeat="x in items track by $index">
- <strong>{{x}}</strong>
- </li>
- </ul>
- <script>
- //使用ng-repeat 重复 html代码
- var app = angular.module('myApp', []);
- app.controller('myCtrl', function ($scope) {
- $scope.items = [1, 2, 3, 2];
- });
- </script>

AngularJs ng-repeat重复项异常解决方案的更多相关文章
- 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 ...
- 关于:未能加载文件或程序集“ICSharpCode.SharpZipLib”或它的某一个依赖项异常的解决方案
问题: 今天项目迁移忽然又个ICSharpCode.SharpZipLib.dll 程序包丢失了,于是我在网上下载一个这样的包,结果程序运行就提示:未能加载文件或程序集“ICSharpCode.Sha ...
- AngularJs ng-repeat解决循环对象出现重复项报错的问题
问题:ng-repeat 的循环对象是不能出现重复项的,所以如果有重复的就会报错,应该是 key value的问题吧,不是很了解内部运行机制:经过查询发现 在 循环后面加上 track by $i ...
- sed tr 去除PATH中的重复项
最近发现由于自己不良的安装软件的习惯,shell的PATH路径包含了很多冗余的项.这里使用shell命令去除PATH的冗余项. export PATH=$(echo $PATH | sed 's/:/ ...
- 【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变
问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1.如果序列中的值时可哈希(hashable)的,可以通过使用集合和生成器解决.
- sql分组合并字段重复项sql for xml path
-------------------------(情景描述) 在我们处理数据时,可能会碰到这种情景: Id Name 1 a,b 2 ...
- 每日一道 LeetCode (8):删除排序数组中的重复项和移除元素
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- 在一般处理程序清理cookie
清理cookie在ashx里面很奇怪,因为直接设置过期时间并不能成功,cookie还是会存在.所以需要添加一个同名的Cookie设置过期时间覆盖 HttpCookie cookie = null; / ...
- 数据库工具SQLite Expert Personal的简单使用
官方网站: sqliteexpert官方网址 - SQLite administration | SQLite Expert 先使用SQLite Expert Personal熟悉SQLite语句 插 ...
- sharepoint 2013 升级要求
1. 安装过程合理: A. 可以同时在管理中心.两台前端.搜索服务器上安装重新发布的SP1补丁包(所提供的链接) B. 等待所有SP1补丁包安装完成,依次在管理中心.两台前端.搜索服务器上运行配置向导 ...
- django系列7.2--django中的cookie和session基本操作,浏览器登陆验证的不同实现
django中的cookie和session(02)–操作 一.Django中的cookie操作 ctrl + shift + del 是谷歌浏览器中清除页面缓存和cookie的快捷键 1.设置coo ...
- CF79D Password
题目链接 题意:给定长度为n的0/1序列,初始值都为0.你每次可以在给定的l个长度中的\(a_i\)并将序列中长度为\(a_i\)的部分取反.使得最终状态为\(x_1\)~\(x_k\),求最少取反次 ...
- 【Oracle 12c】最新CUUG OCP-071考试题库(55题)
55.(13-3) choose the best answer: Which statement is true regarding the SESSION_PRIVS dictionary vie ...
- 视频video遇到一些坑
1.video层级最高问题 解决方案思路:当点击其他按钮触发事件时,视频层级挡住其他外层,比如会挡住弹窗,这是应该让视频暂停播放且隐藏,这是视频这个地方会空一个位置可以放一张封面占位,这样就解决视频层 ...
- luoguP5068 [Ynoi2015]我回来了
https://www.luogu.org/problemnew/show/P5068 ynoi 中的良心题啊 考虑用 bitset 来维护里一个点距离小于 $ y_i $ 的点,那么答案就是一堆 b ...
- 在myeclipse中有的项目上有个红色感叹号
之前做项目的时候遇到过这个问题,最后确定原因是项目引用了很多放在D盘或E盘上的jar包,但是我们不小心把这些jar包删除或移动路径了,因此myeclipse识别不了出现红色的感叹号,解决方式是在mye ...
- php全局变量漏洞 $GLOBALS
在Discuz代码中有这么一段: if (isset($_REQUEST[‘GLOBALS’]) OR isset($_FILES[‘GLOBALS’])) { exit(‘Request tain ...


