我不完全知道什麼是錯的,但我試圖從angular doc申請搜索系統沒有成功。Angular:Observable不工作
以下是該組件所使用的功能:
// my-comp.component.ts
public testing: Observable<any>;
private searchTerms = new Subject<string>();
search(term: string): void {
this.searchTerms.next(term.toUpperCase());
}
此功能每一個我在搜索框所以沒有問題就在這裏輸入一些時間擊中。
這裏是ngOnInit方法:
// my-comp.component.ts
ngOnInit(): void {
this.testing = this.searchTerms
.debounceTime(300) // wait 300ms after each keystroke before considering the term
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time the term changes
// return the http search observable
? this.refundCaseService.search(term)
// or the observable of empty heroes if there was no search term
: this.test(term)
)
.catch(error => {
// TODO: add real error handling
console.log(error);
return Observable.of<any>();
});
}
test(term: string) {
console.log(term);
return Observable.of<any>();
}
永遠達不到這個代碼,它不進入服務既不是測試功能(我用的斷點和執行console.log確保)。
我不明白這是如何工作的。
使用: 角4.0.0 角CLI 1.0.6
所以你訂閱somehwere this.testing? – chrigu