【发布时间】:2017-06-20 12:24:01
【问题描述】:
我不完全知道出了什么问题,但我正在尝试从 angular doc 应用搜索系统,但没有成功。
这是组件中使用的函数:
// 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 Angular-cli 1.0.6
【问题讨论】:
-
所以你订阅了 this.testing?
标签: angular typescript