简体   繁体   English

不允许在转发器中避免重复

[英]Avoid duplicates in a repeater are not allowed

Is there any built-in feature on AngularJS to avoid ng-repeater to receive duplicated entries? AngularJS上是否有任何内置功能可以避免ng-repeater接收重复的条目?

Right now I'm using the following code to prevent it: 现在我正在使用以下代码来阻止它:

$scope.tags = ['black','white','red','yellow','blue'];
$scope.selectedTags = [];    

// textarea value
var words = $scope.message.split(' ');

for(var j = 0; j < words.length; j++) {
    for (var k = 0; k < $scope.selectedTags.length; k++) {
        if ($scope.selectedTags[k].Name == words[j]) {
            contains = true;
        }
    }

    if (!contains)
    {
        $scope.selectedTags.push($scope.tags[i]);
        contains = false;
    }
}

Angular UI has a unique filter: Angular UI有一个unique过滤器:

Filters out all duplicate items from an array by checking the specified key 通过检查指定的键过滤掉数组中的所有重复项

Alternatively, if it's just a string array you can filter your array like: 或者,如果它只是一个字符串数组,您可以过滤您的数组,如:

arr.filter(function (e, i, arr) {
    return arr.lastIndexOf(e) === i;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM