Angularjs - 从控制器更新选择模型会产生错误

时间:2016-11-30 13:52:11

标签: angularjs

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

我错过了什么?

1 个答案:

答案 0 :(得分:0)

ng-repeat中将project in projects track by $index替换为track by此处https://docs.angularjs.org/api/ng/directive/ngRepeat