这是我的方案
我想反应搜索
如果值length > 2
执行api
搜索
否则清楚数据。
但我不知道清除数据
userList: Observable<User[]>;
当length&lt; = 2
时,userList应为空另一个问题是为什么Rxjs
操作符不能在角度2中工作?
this.userList = this.userNameCtrl.valueChanges.do(d => console.log(d));
这是我的代码:
this.userList = this.userNameCtrl.valueChanges
.filter(x => x.length > 2)
.debounceTime(400)
.distinctUntilChanged()
.switchMap(value => this._userService.searchUsers(value));
答案 0 :(得分:1)
如果值长度&gt;我想反应搜索2做api搜索其他明确的数据。
如果我通过清除理解你的意思是将空阵列推向流:
this.userList = this.userNameCtrl.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.switchMap(value => {
if(value.length > 2) {
return this._userService.searchUsers(value);
} else {
return Observable.of([]);
}
});
另一个问题是为什么Rxjs操作符不能在角度2中工作?
this.userList = this.userNameCtrl.valueChanges.do(d => console.log(d));
此代码不输出任何内容,因为您需要订阅可观察的第一个
this.userList = this.userNameCtrl.valueChanges
.do(d => console.log(d))
subscribe(() => {});
// or simply
this.userList = this.userNameCtrl.valueChanges
subscribe(d => console.log(d));