gpt4 book ai didi

javascript - 存在于另一个数组中的过滤器选项

转载 作者:行者123 更新时间:2023-11-30 12:32:00 25 4
gpt4 key购买 nike

我有两个数组,countriesselectedCountries

用户可以将国家countries添加到selectedCountries

现在,我只想显示用户没有已经选择的国家(selectedCountries中的国家)——但我不知道如何实现。

这是 HTML:

<div ng-controller="MyCtrl">
<select
ng-model="selectedCountry"
ng-options="country.name for country in countries"></select>
<button ng-click="addCountry(selectedCountry)">Add country</button>

<table>
<tr ng-repeat="country in selectedCountries">
<td>{{ country.name }}</td>
</tr>
</table>
</div>

和当前的 Javascript:

function MyCtrl($scope) {
$scope.countries = [
{ id: 1, name: 'Denmark' },
{ id: 2, name: 'Sweden' },
{ id: 3, name: 'Norway' }
];

$scope.selectedCountries = [];

$scope.addCountry = function(country) {
$scope.selectedCountries.push(country);
}
}

如何过滤显示的国家

这是一个有效的 JSFiddle

最佳答案

为它创建一个过滤器。您的 HTML:

        ng-options="country.name for country in countries |notin:selectedCountries"

还有你的过滤器:

app.filter('notin',function(){
return function(items,matchset) {
return items.filter(function(item){
return matchset.indexOf(item) < 0;
});
};
});

它会自动使它们保持最新状态。在这里更新了 fiddle :http://jsfiddle.net/jxwbwjdq/1/

关于javascript - 存在于另一个数组中的过滤器选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com