2014-11-05 14 views
5

ng-repeat, ile bir kod test ediyorum Ama açısal eski sürümü ile çalışır, ancak en son sürümü ile çalışmıyor!ng-yineleme öğeleri ekle (AngularJs 1.2.26)

açıklayabilirim: Bu kodu sınamak

: Ben severarls öğeleri eklediğinizde

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.0/angular.js"></script> 

<div ng-app="myApp"> 
    <div ng-controller="MyCtrl"> 
     <ul> 
      <li ng-repeat="item in items">{{item}}</li> 
     </ul> 
     <input ng-model="newItem" type="text"></input> 
     <button ng-click="add(newItem)">Add</button> 
    </div> 
</div> 

<script> 
var app = angular.module('myApp', []); 

app.controller('MyCtrl', function($scope) { 
    $scope.items = ["A", "B", "C", "D"]; 
    $scope.add = function(item) { 
     $scope.items.push(item); 
    }; 

}); 
</script> 

, iyi çalışıyor! angular.js/1.1.0 sürümüyle

Yeni bir öğe ekleyiniz Ancak en son sürümde çalışmıyor! Biz bir öğe eklemek, ama biz daha fazla öğe eklerseniz, bu hatayı yapar:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in items, Duplicate key: string:d

Benim soru biz haber sürümleri ile ng-tekrarında haber öğelerini ekleyebilir nasıl?

Teşekkürler! Aşağıda

<li ng-repeat="item in items track by $index"> 

Demo:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <div ng-controller="MyCtrl"> 
 
     <ul> 
 
      <li ng-repeat="item in items track by $index">{{item}}</li> 
 
     </ul> 
 
     <input ng-model="newItem" type="text"></input> 
 
     <button ng-click="add(newItem)">Add</button> 
 
    </div> 
 
</div> 
 

 
<script> 
 
var app = angular.module('myApp', []); 
 

 
app.controller('MyCtrl', function($scope) { 
 
    $scope.items = ["A", "B", "C", "D"]; 
 
    $scope.add = function(item) { 
 
     $scope.items.push(item); 
 
    }; 
 

 
}); 
 
</script>

+1

cevap söz konusu olduğunu! :) 'track by' kullanın, buradaki gibi: http://stackoverflow.com/questions/26232764/angularjs-nested-ng-repeat-array-in-object-only-works-if-there-is-one-item -in/26232889 # 26232889 – Cherniv

cevap