如何在angularjs中使用ng-repeat将数据推送到对象数组中时阻止重复

问题描述

我正在尝试使用ng-repeat动态添加div元素。

    <div class="dynamicRow">
    <div class="col-sm-6">
    <input class="form-control" ng-disabled="true" type="text" ng-model='item.name' id='item-{{$index}}' class='input-{{$index}}'/>
    </div>
    </div>
    </div>

如果我将ng-repeat与track by item.id一起使用,则不允许重复,但是一旦获得重复异常,此后我也无法添加任何新项目。

  $scope.items=[];

  $scope.addAnother = function(){

    if ($scope.items.length < 10) {
              $scope.items.push({  
                  "id": $scope.userid,"name": $scope.username  
              });
             }
    
    }  

单击按钮即可调用功能。 项目形式为 items = [{id:“ 001”,名称:“ A PLUS”},{id:“ 0012”,名称:“ TEST”},{id:“ 0013”,名称:“另一个测试”}] 这里的数据是动态的,仅当单击按钮时,数据才会被推送。现在使用代码,不允许重复,但是如果遇到重复,那么我也无法添加任何其他值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)