我有一个如下所示的数据模型(还有其他字段,但这些是最重要的):
$scope.model = [
{
id: 1,
country: "US"
},
{
id: 2,
country: "UK"
},
{
id: 3,
country: "DE"
},
{
id: 4,
country: "DE"
},
{
id: 5,
country: "DE"
},
{
id: 6,
country: "US'
},
]
我想得到以下数组:
[ "US", "UK", "DE"]
为了在 <select>
中显示它像下面这样:
<select ng-model="myList" ng-options="m.country for m in model | somefilter "><select>
unique 并不方便,因为它的作用与我想要的完全相反。
最佳答案
除非我误解了您想要通过模型数组中对象的唯一属性进行过滤的问题。 唯一的过滤器通过传入属性键来实现此目的:
<select ng-model="selected" ng-options="m.country for m in model | unique: 'country'">
<option value="">choose country</option>
</select>
关于javascript - AngularJS 使用 ng-options 创建 Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25744868/