AngularJS 101:使用ng-repeat的ng-init - 在这个例子中我缺少什么?

时间:2017-03-16 15:19:23

标签: angularjs

Below snippet doesn't run. 
What am I missing here with ng-init?!! 
If I change array values to 'non-repeating' values it works.
For example, changing 
names=['Jani','Hege','Kavi','Kavi2'] 
it works.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<body>

<div ng-app="" ng-init="names=['Jani','Hege','Kavi','Kavi']">
  <p>Looping with ng-repeat:</p>
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

track by $index:您可以查看文档 - Tracking and Duplicates

<li ng-repeat="x in names track by $index">
      {{ x }}
 </li>