【发布时间】:2017-04-28 13:03:59
【问题描述】:
我正在尝试找到最干净的解决方案来使用 filter() 运算符来过滤我的 observables。
这里我正在复制服务调用以分别获取femaleList
export class MyComp implements OnInit {
maleList: IContact[] = [];
femaleList: IContact[] = [];
constructor(private _contactService: ContactService) { }
ngOnInit() : void {
this._contactService.getContacts()
.filter(male => male.gender === 'M')
subscribe(maleList => this.maleList = maleList);
this._contactService.getContacts()
.filter(female => female.gender === 'F')
subscribe(femaleList => this.femaleList = femaleList);
} }
联系人列表
[{
"id" : 1,
"name" : "Todd",
"gender" : "M"
}, {
"id" : 2,
"name" : "Lillian",
"gender" : "F"
}]
RxJS 运算符中是否有任何选项可以将单个 observable 分配给两个变量。
如何过滤联系人并将其分配给 maleList 和 femaleList 使用 RxJS filter() 运算符。
提前致谢
【问题讨论】:
标签: angular rxjs angular2-services rxjs5 rxjs-dom