1

I have an array

$scope.answers=["","","",""]

means Array have multiple empty elements .

<li ng-repeat="item in answers "><input type="text" ng-model="item"/></li>

it throws error that duplicate values are not allowed in ng-repeat . if i use

<li ng-repeat="item in answers track by $index"><input type="text" ng-model="item"/></li>

then its working fine . but i want to use this without track by $index so that sorting on this is also work

Can anybody have idea about this

Tip-Sy
  • 810
  • 10
  • 18
Sushil
  • 11
  • 1

2 Answers2

0

Inspired from this

Add this to your controller (as you can't access angular object (other than scope's ones) in your HTML markup)

$scope.identity = angular.identity;

Then use the following in your HTML and angular will not bother you anymore :

<li ng-repeat="item in answers | orderBy : identity track by $index"><input type="text" ng-model="item"/></li>
Community
  • 1
  • 1
yunandtidus
  • 3,847
  • 3
  • 29
  • 42
0

Why don't you use objects inside the array like so:

$scope.answers = [{}, {}, {}, {}, {}];

Then in your view use a property of the empty object.

<li ng-repeat="item in answers">
    <input type="text" ng-model="item.content"/>
</li>

You can see this in action in the following jsFiddle.

shxfee
  • 5,188
  • 6
  • 31
  • 29