2015-05-18 2 views
0

Я использую ng-repeat для вывода строки в массиве.Дубликаты в ретрансляторе, отслеживание не помогает

массив выглядит так:

"whatToDo": [ 
     "Many people with Learning Disabilities aren’t aware of their rights. It could be helpful for them to know more about their human rights.", 
     "Involve relevant services that may be able to provide support to the person, for example their Community Learning Disability Nurse or Social worker." 
    ] 

Когда я выходной массив так:

<p ng-repeat="todos in data.whatToDo">{{todos}}</p> 

Я получаю ошибку:

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: todos in data.whatToDo, Duplicate key: string:v, Duplicate value: v 

Когда я использовать рекомендованное решение от:

<p ng-repeat="todos in data.whatToDo track by $index">{{todos}}</p> 

Он печатает только первую букву каждого элемента массива.

У меня есть несколько экземпляров массива whatToDo.

Благодаря

+1

здесь что-то не хватает. Это работает отлично, с или без «трека». http://plnkr.co/edit/LKs6o8qDrRsWTEFVXNxx?p=preview/. Пожалуйста, покажите полный объект, содержащий этот массив. – Claies

+0

Вы можете поделиться кодом функции контроллера/ссылки? –

+0

Вы можете создать jsfiddle/plunkr, который воспроизводит проблему? – yarons

ответ

1

вот рабочий пример того, как цикл через массив и массив объектов: http://jsfiddle.net/usxmtjnh/3/
копия этого кода below..see, если это помогает:

HTML:

<div ng-app="app" ng-controller="Cntrl"> 
     <h3>if you have an array:</h3> 
     <p ng-repeat="text in whatToDo">{{text}}</p> 

     <br><hr> 

     <h3>if you have an array of objects:</h3> 
     <p ng-repeat="otext in whatToDoObjectCollection">{{otext.line}}</p> 
</div> 

ЯШ:

angular.module("app", []).controller("Cntrl", function ($scope){ 

    //array 
    $scope.whatToDo = [ 
    "Many people with Learning Disabilities aren't aware of their rights. It could be helpful for them to know more about their human rights.", 
    "Involve relevant services that may be able to provide support to the person, for example their Community Learning Disability Nurse or Social worker." 
    ]; 

    //array of objects 
    $scope.whatToDoObjectCollection = [ 
     {line: "Many people with Learning Disabilities aren't aware of their rights. It could be helpful for them to know more about their human rights."}, 
     {line: "Involve relevant services that may be able to provide support to the person, for example their Community Learning Disability Nurse or Social worker."} 
    ]; 
}); 
+0

Спасибо, это был стиль 'whatToDoObjectCollection', который мне нужно было использовать – Alan

+0

рад, что я мог бы помочь Алану. GL с вашим приложением –

Смежные вопросы