Angular 6自动建议搜索框
我一直在学习这个教程,我想在angular 6中完成它 如何将其转换为与angular 6兼容的代码Angular 6自动建议搜索框,angular,typescript,rxjs,Angular,Typescript,Rxjs,我一直在学习这个教程,我想在angular 6中完成它 如何将其转换为与angular 6兼容的代码 ngOnInit() { this.queryField.valueChanges .debounceTime(200) .distinctUntilChanged() .switchMap((query) => this._searchService.search(query)) .subscribe( result => { if (re
ngOnInit() {
this.queryField.valueChanges
.debounceTime(200)
.distinctUntilChanged()
.switchMap((query) => this._searchService.search(query))
.subscribe( result => { if (result.status === 400) { return; } else { this.results = result.json().artists.items; }
});
}
}
我自己转换了这个,但在this.logframe.searchEmployee(term.subscribe()
如果您对此有其他选择,请让我知道:)
编辑:
修复了我的问题:)
我只需要通过获得特定的formcontrol来调整Fan Cheung代码,然后还添加catcherror,这样它就不会在null上的404之后完成
ngOnInit() {
this.registerationForm.get('name').valueChanges
.pipe(
debounceTime(200),
distinctUntilChanged(),
switchMap((term) => this.logframe.searchEmployee(term).pipe(catchError(err => of('null')))),
).subscribe(
val => console.log(val)
);
}
ngOnInit() {
this.registerationForm.get('name').valueChanges
.pipe(
debounceTime(200),
distinctUntilChanged(),
switchMap((term) => this.logframe.searchEmployee(term).pipe(catchError(err => of('null')))),
).subscribe(
val => console.log(val)
);
}
订阅返回
Subscription
不能被switchMap
如果this.logframe.searchEmployee(term)
返回可观察值,请尝试下面的代码
ngOnInit() {
this.registerationForm.valueChanges
.pipe(
debounceTime(200),
distinctUntilChanged(),
switchMap((term) => this.logframe.searchEmployee(term)),
);
}
修复了我的问题:)
我只需要通过获得特定的formcontrol来调整Fan Cheung代码,然后还添加catcherror,这样它就不会在null上的404之后完成
ngOnInit() {
this.registerationForm.get('name').valueChanges
.pipe(
debounceTime(200),
distinctUntilChanged(),
switchMap((term) => this.logframe.searchEmployee(term).pipe(catchError(err => of('null')))),
).subscribe(
val => console.log(val)
);
}