几个示例演示如何使用可观察性连接控件以显示从http后端获取的数据,例如:http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html。
在某些情况下,你能阻止http呼叫吗?例如,在上述文章中,有一个自动完成字段--在用户清除字段的情况下,是否有一种方法可以防止http调用?
我尝试过修改switchMap函数,例如:
if(term.length < 1) {
return Observable.empty();
}
else { // call the http service...它确实阻止了调用,但将前面的结果留在控件中。
发布于 2016-01-08 11:58:05
对不起,我在手机上,但是像filter(term => term.length > 0)这样的东西应该能起作用。
评论后的更新
也许有更优雅的方法来处理这件事,但这样如何呢?
this.items = this.term.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.switchMap(term => term.length > 0
? this.wikipediaService.search(term)
: Observable.of([]));工作柱塞:http://plnkr.co/edit/3p9eqiPAcqjBIEdLNkUG?p=preview
https://stackoverflow.com/questions/34670868
复制相似问题