[英]Angular2: how to initially trigger Control.valueChanges
constructor(private _service: LocatorService) {
this.counties = this.countyTerm.valueChanges
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));
}
counties: Observable<County[]>;
countyTerm = new Control();
As expected, this.counties
is only populated once a value is entered into the countyTerm
bound control.正如预期的那样, this.counties
仅在将值输入到countyTerm
绑定控件中时才会填充。
How can I trigger valueChanges
when this component is instantiated so that the set of counties is loaded initially?如何在实例化此组件时触发valueChanges
以便最初加载一组县?
I tried the following, but it had no effect ( implements OnInit
was added to the class):我尝试了以下操作,但没有效果( implements OnInit
已添加到类中):
ngOnInit() {
this.countyTerm.updateValue('', { emitEvent: true });
}
Just start your stream out with a fixed value. 只需以固定值开始播放即可。 Something like this: 像这样:
this.counties = Rx.Observable.of('')
.concat(this.countyTerm.valueChanges.debounceTime(300))
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));
Use startWith RxJS operator to emit something before stream. 使用startWith RxJS运算符在流之前发出一些东西。
this.counties = this.countyTerm.valueChanges
.startwith('')
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => _service.getCounties(term));
http://reactivex.io/documentation/operators/startwith.html http://reactivex.io/documentation/operators/startwith.html
With RxJS 6 / 7 new syntax:使用 RxJS 6 / 7 新语法:
this.counties$ = this.countyTerm.valueChanges.pipe(
startWith(''),
debounceTime(300),
distinctUntilChanged(),
switchMap((term: string) => _service.getCounties(term))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.