Angularjs错误:ng-options中的[ngRepeat:dupes]

时间:2017-05-02 16:04:11

标签: javascript angularjs ng-options

您好我正在制作代码以在angularjs中学习并发现错误。

  

错误:[ngRepeat:dupes]   http://errors.angularjs.org/1.6.4/ngRepeat/dupes?p0=cursostable%20in%20cursos%20%7C%20filter%3ApesquisaCurso&p1=object%3A3&p2=%7B%22codigo%22%3A45434%2C%22descricao%22%3A%22java%20web%22%2C%22datahorario%22%3A%22Segundas%20e%20Sextas%2C%2019hrs%22%2C%22professor%22%3A%7B%22codigo%22%3A1%2C%22nome%22%3A%22asasasasasa%22%2C%22email%22%3A%22asasasas%40hotmail.com%22%2C%22fone%22%3A%222323232323%22%7D%2C%22%24%24hashKey%22%3A%22object%3A3%22%7D

在控制台中

此错误阻止我输入新记录

以下代码:

HTML

      <div class="form-group">
          <label for="professor" class="col-sm-1">Professor</label>
          <div class="col-sm-10">
          <select class="form-control" id="curso.professor"ng-model="curso.professor" 
    ng-options="professor.nome for professor in professores track by professor.codigo">
         </select>
          </div>
                    </div>

    <button type="button" ng-click="salvar()" ng-disabled="!curso.codigo || !curso.descricao || !curso.datahorario || !curso.professor" class="btn btn-success">Cadastrar</button>

控制器

 cursoModulo.controller("cursoController", function($scope){
    $scope.professores = [
        {codigo: 1, nome: 'asasasasasa', email: 'asasasas@hotmail.com', fone:'2323232323'},
    ];

    $scope.cursos = [
        {codigo: 45434, descricao: 'java web', datahorario: 'Segundas e Sextas, 19hrs', professor: {codigo: 1, nome: 'asasasasasa', email: 'asasasas@hotmail.com', fone:'2323232323'}},
    ];

    $scope.selecionaCurso = function(cursoSelecionado){
        $scope.curso = cursoSelecionado;
    }

    $scope.limparCampo = function(){
        $scope.curso = null;
    }

    $scope.salvar = function(){
        $scope.cursos.push($scope.curso);
        $scope.limparCampo();
    }

    $scope.excluir = function(){
        $scope.cursos.splice($scope.cursos.indexOf($scope.curso),1);
        $scope.limparCampo();
    }

3 个答案:

答案 0 :(得分:0)

我想,新记录中有一个“codigo”已经出现在“professores”列表中。请记住角集合中的所有记录必须具有您在“跟踪”表达式中定义的uniq字段

答案 1 :(得分:0)

如果你使用的是角度版本&lt; 1.2.0

将您的版本更改为(最小值)1.2并且它将起作用,因为&#34;跟踪&#34;在该版本中引入。

你的代码在这个小提琴上工作(jsfiddle.net/JKBbV/1370),角度为1.6.4

答案 2 :(得分:0)

AngularJS不允许ng-repeat指令中的重复项。

这意味着您在track by写的任何内容都应该是唯一的,因此请尝试使用track by $index而不是rrd所说的track by professor.codigo

工作Plnkr

来源:click here!