我正在使用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
数组的多个实例。
由于
答案 0 :(得分:1)
这是一个关于如何遍历数组和对象数组的工作示例:http://jsfiddle.net/usxmtjnh/3/
这份代码的副本如下。看看是否有帮助:
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>
JS:
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."}
];
});