Felipe Miosso
Felipe Miosso

Reputation: 7339

Avoid duplicates in a repeater are not allowed

Is there any built-in feature on AngularJS to avoid ng-repeater to receive duplicated entries?

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;
    }
}

Upvotes: 0

Views: 437

Answers (1)

CD..
CD..

Reputation: 74126

Angular UI has a unique filter:

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;
});

Upvotes: 2

Related Questions