Angular2、RC5:在下拉列表中不添加重复的值



我正在从远程服务器获得测试值,GroupID值可以复制,但我希望我的下拉菜单将显示没有重复的值,我尝试了下面的代码,它不工作

<th>
<select class="form-control" (change)="reloadPosts({ GroupID: u.value })" #u>
<option value="">Select group id...</option>
<template *ngFor="let test of tests"> <option *ngIf="!test" value="{{ test.GroupID }}">
{{ test.GroupID }}
</option></template>
</select>
</th>   

如何在angular rc5及以上版本中做到这一点

谢谢

您应该能够使用一个简单的管道…

@Pipe({ name: 'unique', pure: false })
export class UniquePipe implements PipeTransform {
    transform(value: any, args: any[] = null): any {
        return _.uniq(value);
    }
}
然后在你的html中使用*ngFor="let test of tests | unique"

管道示例使用了下划线.js,但您可以随意以任何方式编写过滤器。

关于管道的更多信息

我无法使用管道解决这个问题,所以我修改了我的组件代码,看到的:Angular2,创建新的数组/map

最新更新