constructor(private _service: LocatorService) {
this.counties = this.countyTerm.valueChanges
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));
}
counties: Observable<County[]>;
countyTerm = new Control();
正如预期的那样,只有在this.counties
绑定控件中输入值后才会填充countyTerm
。
如何在实例化此组件时触发valueChanges
,以便最初加载这组县?
我尝试了以下操作,但没有效果(implements OnInit
已添加到课程中):
ngOnInit() {
this.countyTerm.updateValue('', { emitEvent: true });
}
答案 0 :(得分:4)
只需使用固定值启动流即可。像这样:
this.counties = Rx.Observable.of('')
.concat(this.countyTerm.valueChanges.debounceTime(300))
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));
答案 1 :(得分:1)
使用startWith RxJS运算符在stream之前发出一些东西。
this.counties = this.countyTerm.valueChanges
.startwith('')
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));