0

I'm having problems on filtering an array of String with ng-options

Assuming I have an array of this type

$scope.params.output = ["A","B","A","B","A","B"];

and an HTML of this type:

ng-options="item as item for item in params.output | unique : 'item'"

my select renders all the array, without filtering it, where did i get wrong?

Anon
  • 502
  • 5
  • 19

2 Answers2

0

Add unique.js in your code.Copy this js and place it in seperate js file and attach it to your code

Alexa
  • 77
  • 11
0

use this filter. it will work.

app.filter('unique', function() {
    return function(collection, keyname) {
        var output = [], 
        keys = [];

        angular.forEach(collection, function(item) {
            var key = item[keyname];
            if(keys.indexOf(key) === -1) {
                keys.push(key);
                output.push(item);
            }
        });

        return output;
    };
});

Template:

<div ng-repeat="item in items | unique: 'id'"></div>
Avnesh Shakya
  • 3,828
  • 2
  • 23
  • 31
pankaj malik
  • 107
  • 2
  • 15
  • this code will work for array of object and you want to get unique object. for array of string there is a minor modification in code. – pankaj malik Sep 25 '17 at 10:57