1.当 ng-repeat 的数组被替换时, 它默认并不会从新利用已有的 Dom 元素,而是直接将其所有删除并从新生成新的数组 Dom 元素:后端
2.Dom 的频繁操做是很是不友好的, ng-repeat为何不能利用已有的 dom 元素去更新数据呢?由于你没有把数组元素的标识属性告诉它,那么两次替换的时候它就没办法追踪了;数组
3.ng-repeat会为每个元素加上一个hashkey $$hashKey来识别每个元素,当咱们从后端从新获取数据时,即便数据彻底同样,可是因为hashKey不同,dom
angular会删除以前的全部dom,从新生成新的dom,这样效率就会大大下降。能够理解为ng-repeat默认是 track by $$hashKey的。spa
因此,咱们应该使用一些不会变的东西来做为标识,好比后端数据的id.code
4.列如:blog
<li class="gwclist" ng-repeat = "gwc in data | orderBy:'-sqtime' track by gwc.ID" ng-click = "gwcdetail(gwc)"> <h5>申请人:{{gwc.sqname}}<span class="pull-right color-dimgray">{{gwc.sqtime}}</span></h5> <span>用餐时间:{{gwc.mettime}}</span>
<p ng-class="{0:'color-lightyellow',1:'color-red',2:'color-blue',3:'color-dimgray'}[{{gwc.processState | showstyle}}]">流程状态:{{gwc.processState}}</p> <div class="button-bar" ng-if = "gwc.processState == '未提交'"> <a class="button button-small button-positive" ng-click="submit(gwc.ID)">提交</a> <a class="button button-small button-stable" ng-click="delete(gwc.ID)">删除</a> </div> </li>
优势:这样当从新获取数据时,因为ID没有变,angular就不会去删除原来的dom,只会更新其中的内容,不一样的ID再添加新的dom。效率就能提高了;
5.注意:qt
track by要放在orderBy以后,不然会影响orderBy的效果;hash
当单一数组有重复元素时,须要使用track by $index来保证两个元素都会渲染,不然只会渲染一个;it