我试图在选择框上的 Angular ng-options 中获得独特的结果。
<select ng-model="nationality" id="nationality"
ng-options="player as player.nationality for player in players></select>
上面的代码将为我提供所有国籍,但它显示重复项。
<select ng-model="nationality" id="nationality"
ng-options="player as players | unique: 'player.nationality'"></select>
当我尝试此代码时, Angular 抛出错误并且在选择框中没有显示任何内容。
Controller
var app = angular.module('premierLeagueDB', []);
app.controller('mainCtrl', function($scope, $http) {
$http({
method : "GET",
url : "api.php/clubs",
}).then(function mySucces(response) {
$scope.clubs = response.data.club;
}, function myError(response) {
$scope.showError = response.statusText;
});
});
谢谢。
最佳答案
我认为这段代码适合你:
<select ng-model="nationality" id="nationality" ng-options="player as players | unique: 'nationality' "></select>
===编辑===
基于此post ,您需要第三方库为您提供filter
服务,包括this library在你的 html 文件中。
==编辑2=== 为了演示这是如何工作的,我做了一个 working fiddle 。在此 fiddle 中,我使用 anuglar-ui 库来替换模块,并且它按预期工作。
如果您只需要 'ui.filters' 模块而不需要其他部分(仅导入必要的模块是最佳实践),这里是 another fiddle link仅从 ui-filter
模块导入 unique
函数。
关于javascript - Angular 中独特的 ng 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809291/