Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Can you delete empty element from your array ? –  Mathieu Bertin Oct 2 at 8:25

2 Answers 2

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>
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.