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 ...
随机推荐
- 自己从0开始学习Unity的笔记 VI (C#的for循环练习)
最近学到了for循环,我觉得其实看情况吧,和while挺像的,不过适合于累加或者累减这类的,for循环要更好用一点 for循环首先格式是 ; i < length; i++) { } 意思很简单 ...
- 重新打造的我的Pugo
Pugo博客已经搭建了好几个月了吧,给我感受到非常方便的就是,我换了好多VPS,迁移显得非常的方便,但是也有不足的地方,比如发布一篇新的博客,我每次都需要重新进入后台进行新的Post,还需要重新bui ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(23)
23.choose the best answer View the Exhibits and examine PRODUCTS and SALES tables. You issue the fol ...
- Java找那个io类-File获取功能
package com.hxzy.IOSer;import java.io.*; /* * File 的获取功能 * */public class Demo04 { public static voi ...
- Java Web 学习与总结(一)Servlet基础
配置环境:https://www.cnblogs.com/qq965921539/p/9821374.html 简介: Servlet是Sun公司提供的一种实现动态网页的解决方案,在制定J2EE时引入 ...
- robot framework-接口测试实例一
需求:api/car/detail/recommendcar.json 接口返回的车辆数量少于等于20且车辆不能重复 分析:统计接口中返回的列表的长度,再把carid拿出来组成一个新的列表,判断这 ...
- flask后端获取前端post/get数据
post:用request.form 而且要加上return !!记着加上return get:用 request.args()就可以了
- [iOS笔试600题]二、常识篇(共有72题)
[B]1.NSObject是一个根类,几乎所有的类都是从它派生而来.但是根类并不拥有真它类都有的alloc和init方法?[判断题] A. 正确 B. 错误 [A]2. UIResponder可以让继 ...
- L07-Linux配置ssh免密远程登录
本文配置可实现:集群服务器之间相互可以ssh免密登录.若只想从单一机器(如master)ssh免密登录其他机器(slave1.slave2),则只跟着操作到第二步即可. 建议先花两三分钟把全文看完再跟 ...
- Go语言指针
指针简介 (Pointer)是编程语言中的一个对象,利用地址,它的值直接指向(points to)存在电脑存储器中另一个地方的值.由于通过地址能找到所需的变量单元,可以说,地址指向该变量单元.因此,将 ...


