有什么方法可以限制ng-repeat中的重复项目的数量?

有没有什么办法可以限制AngularJS中重复的项目数量? 就像在联系人列表中,我需要input相同的姓名和联系电话号码,但只有2次,当我试图进入第三次时,它显示我一些警告,你已经input联系方式。 我怎样才能通过使用AngularJS或JavaScript来实现这个任务?

我的代码是

<md-list-item ng-show="showContactList" ng-repeat="numbers in contactList track by $index" > <i ng-show="numbers.type == 'test'">textsms</i> <i ng-show="numbers.type == 'CELL' || numbers.type == 'EXT'">phone</i> <img ng-show="numbers.type == 'PAGER'" src="pager.png" width="26"> <div class="md-list-item-text" ng-class="{'md-offset': phone.options.offset }"> <h3>{{ numbers.type }}</h3> <p>{{ numbers.value }}</p> </div> <i ng-click="arrayText.push(numbers);">add</i> </md-list-item> 

在上面的代码中,我可以添加N号码来联系姓名和号码。

尝试这个

HTML

  <i class="material-icons md-avatar-icon add-rm-icon margin-right" ng-click="insertRecord(numbers);">add</i> 

调节器

  var existed = 0; $scope.insertRecord = function(numbers){ var name = numbers.type; angular.forEach($scope.arrayText, function(value, key) { var arr = Object.values(value); if(arr.indexOf(name) !== -1 ) { existed++; console.info(existed); } }); if(existed >= 2){ console.info('already exist') existed = 0; }else{ $scope.arrayText.push(numbers); existed = 0; } } 

var namevariables中,你可以把你想要检查2次插入的属性。

你可以用下面的代码来限制显示项目。

在HTML模板绑定

  {{ limitTo_expression | limitTo : limit : begin}} 

在控制器中

  $filter('limitTo')(input, limit, begin) 

有关更多详细信息,请参阅https://docs.angularjs.org/api/ng/filter/limitTo

为插入创build函数中的限制项目

 <i class="material-icons md-avatar-icon add-rm-icon margin-right" ng-click="pushFunction(numbers);">add</i> 

在控制器中

 $scope.pushFunction = funcation(numbers){ // check number exist $scope.existedNumbers = $scope.arratext.filter(function (objArray) { return (objArray == numbers); }); if($scope.existedNumbers && $scope.existedNumbers.length<2) $scope.arrayText.push(numbers); else // show alert };