如何从 angular ui select 中删除重复的选项?

How to remove duplicate options from angular ui select?

这是我的angularuiselecthtml:

<ui-select title="Select Template Directory" ng-change="searchTemplate()" theme="select2" ng-model="search_data.sub_directory" id="search_sub_directory" name="search_sub_directory" class="search_panel">
    <ui-select-match allow-clear="true" placeholder="Select Template Directory">{{$select.selected.subDirectory}}</ui-select-match>
    <ui-select-choices repeat="template.subDirectory as template in response_template_directories | filter: $select.search">
        <span ng-bind-html="template.subDirectory | highlight: $select.search"></span>
    </ui-select-choices>
</ui-select>

这里是response_template_directories

[
{subDirectory : 1},
{subDirectory : 1},
{subDirectory : 1},
{subDirectory : 1},
{subDirectory : 1},
{subDirectory : 1},
{subDirectory : 2},
{subDirectory : 3},
]

Select 列表也显示重复项

如何删除 ui select html 中的这些重复项?

使用唯一过滤器(从 AngularUI or import a8m/angular-filter 实施)。

<ui-select title="Select Template Directory" ng-change="searchTemplate()" theme="select2" ng-model="search_data.sub_directory" id="search_sub_directory" name="search_sub_directory" class="search_panel">
<ui-select-match allow-clear="true" placeholder="Select Template Directory">{{$select.selected.subDirectory}}</ui-select-match>
<ui-select-choices repeat="template.subDirectory as template in response_template_directories | unique: 'subDirectory' | filter: $select.search">
    <span ng-bind-html="template.subDirectory | highlight: $select.search"></span>
</ui-select-choices>

或者用 lodash 编写你自己的过滤器:

app.filter('unique', function() {
    return function (arr, field) {
        return _.uniq(arr, function(a) { return a[field]; });
    };
});