2017-07-18 21 views
1

私はこのような選択要素がある場合:同じ値を持つ複数のStrinsを持っている可能性を持っているオブジェクトのリストから要素を選択するに移入selectで重複する文字列を避けるには?

​​

を、どのように私は重複したエントリを表示するために回避することができます?

+3

の可能性のある重複した[重複した結果をNG-繰り返しフィルタを作る方法](https://stackoverflow.com/questions/15914658/how- make-ng-repeat-filter-out-duplicate-results) –

答えて

1

使用一意のフィルタUnique-filter

angular.module("app",['angular.filter']) 
 
    .controller("ctrl",['$scope',function($scope){ 
 
    $scope.searchFilterDispatcher={}; 
 
    $scope.searchFilterDispatcher.params = [ 
 
     {output:'abc', value:1}, 
 
     {output:'abc', value:1}, 
 
     {output:'xyz', value:2} 
 
    ]  
 
    
 
    }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.16/angular-filter.js"></script> 
 
<html> 
 
<body ng-app="app" ng-controller="ctrl"> 
 
    <select 
 
    ng-model="dispatchOutput" 
 
    ng-options="item as (item.output | uppercase) for item in searchFilterDispatcher.params | unique : 'output'" 
 
    class="form-control" 
 
    id="dispatchOutput" > 
 
</select> 
 
</body> 
 
<html>

1

あなたは一意の値をフィルタリングし、NG-オプションにそれを適用するには、カスタムフィルタを作成する必要があります。

app.filter('unique', function() { 
    return function (arr, field) { 
     return _.uniq(arr, function(a) { return a[field]; }); // (Assuming that you are using lodash in your app) if not use you custom logic here 
    }; 
}); 

後でテンプレートであなたはこのフィルタを使用することができます。

しかし、私はあなたがhttps://github.com/a8m/angular-filter#unique

これは、使用のためにすでに使用可能なフィルタをたくさん持っているし、あなたの上記の目的を果たすことができます使用することをお勧めします。

関連する問題