HTML代码:
<select ng-model='currentProject' ng-repeat='project in projects'>
<option value=''>Choose a project</option>
</select>
变量项目是一个字符串数组。现在,当我向控制器中的项目添加项目并更新模型时,我得到一个错误,该数组有重复。
在控制器中:
$scope.projects.push($scope.newProject);
$scope.currentProject = $scope.newProject;
AngularJS抛出的错误:
Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: project in projects
我错过了什么?
答案 0 :(得分:0)
在ng-repeat
中将project in projects track by $index
替换为track by
此处https://docs.angularjs.org/api/ng/directive/ngRepeat