/ / AngularJS - „Niedozwolone duplikaty w repeaterze” - javascript, angularjs, angularjs-ng-repeat

AngularJS - "Duplikaty w repetytorach niedozwolone" - javascript, angularjs, angularjs-ng-repeat

Próbuję utworzyć HTML textarea dla każdego elementu tablicy, ale nie jest renderowany. Przeczytałem dokumenty AngularJS i próbowałem różnych track by wyrażenia bezskuteczne.

Warunek błędu powstaje, jeśli użytkownik wpisze ten sam tekst w wielu miejscach textareas. Wygląda na to, że Angular używa wartości pola tekstowego jako unikalnego klucza. Mogę to zhakować, dodając i usuwając dowolny tekst przed tekstem, ale wydaje się to kiepskie.

Aby wyjaśnić wyświetlanie pól tekstowych, problem występuje po kliknięciu przez użytkownika przycisku Prześlij (tylko javascript - bez podróży w obie strony na serwer), a następnie kolejny kod renderuje to, co wpisał użytkownik.

Z góry dziękuję.

Konkretny błąd, który otrzymuję to:

Duplicates in a repeater are not allowed.
Use "track by" expression to specify unique keys.
Repeater: openEndedQuestion in openEndedQuestions track by openEndedQuestion.id,
Duplicate key: undefined

HTML to:

<tr ng-repeat="openEndedQuestion in openEndedQuestions">
<td width="375" style="font-size: 18px;">
<b>{{openEndedQuestion}}</b><br>
<textarea id="openEndedAnswer_{{$index}}" cols="80"></textarea>
</td>
</tr>

Drugi etap HTML to:

<tr ng-repeat="openEndedAnswer in openEndedAnswers">
<td width="375" style="font-size: 18px;">
<b>{{openEndedAnswer}}</b>
</td>

<td width="225">
<span style="font-size: 35px; padding: 2px; color: #468847;"
ng-repeat="starIndex in [0, 1, 2, 3, 4]"
ng-click="povStarTracker.setStarRating($parent.$index, starIndex + 1)"
ng-class="povStarTracker.getClassForStar($parent.$index, starIndex)"></span>
</td>
</tr>

Podstawowy JSON to:

"openEndedQuestions" : [
"Do you think the coach is being fair?",
"Should the coach give all of his players a chance to play in the games?",
"Should Austin say anything about this to his coach?",
"What could Ryan say to Austin?"
]

Odpowiedzi:

5 dla odpowiedzi № 1

Zmień lub dodaj track by część do twojego powtórzenia ng

albo użyć by $index lub by openEndedQuestion

 <tr ng-repeat="oeq in openEndedQuestions track by oeq">

lub

 <tr ng-repeat="oeq in openEndedQuestions track by $index">

Lepiej użyć drugiego przypadku

Również z reguły staraj się nie wiązać prymitywów pod kątem (tj. Ciągów i liczb). Lepszym rozwiązaniem mogłoby być:

w kontrolerze:

$scope.questions = [

{id:1, text: "Do you think the coach is being fair?"},
{id:2, text: "Should the coach give all of his players a chance to play in the games?"},
{id:3, text: "Should Austin say anything about this to his coach?"},
{id:4, text: "What could Ryan say to Austin?"}
]

i w szablonie:

 <tr ng-repeat="q in questions track by q.id">

Zobacz jsfiddle: http://jsfiddle.net/vittore/V5n4t/