Angular2 Rxjs keyup如何连接字符

时间:2016-12-06 21:05:52

标签: angular rxjs5

我的目标是结束角色 输入这样的输入

<input #term (keyup)="search()">

所以我尝试了

const obs$: Observable<KeyboardEvent> = Observable.fromEvent(this.getNativeElement(this.term), 'keyup');
    obs$
    .debounceTime(500)
    .map(ev => ev.key)
    .distinctUntilChanged()
    .scan((acc, one) => acc + one)
    .do(x => console.log(x))
    .subscribe(term => this.search(term));

并且在您不使用退格键之前一切都很好 只是一个抓住要点的例子

a
app.component.ts:49 av
app.component.ts:49 avf
app.component.ts:49 avfd
app.component.ts:49 avfdBackspace
app.component.ts:49 avfdBackspaced
app.component.ts:49 avfdBackspaceds
app.component.ts:49 avfdBackspacedsw
app.component.ts:49 avfdBackspacedswControl

那么我可以在没有得到正确输入的情况下使用它?

更新

我可以这样做

term$ = new Subject<string>();
(input)=term$.next($event.target.value)

但是我想用fromEvent

来做

UPDATE2(见@meligy的回复)

Observable.fromEvent<HTMLInputElement>(this.getNativeElement(this.term), 'keyup')
    .debounceTime(500)
    .map(ev => ev.target.value)
    .distinctUntilChanged()
    .do(termDebug => console.log(termDebug))
    .switchMap(term => this.service.search(term))
    .subscribe(result => this.items = result);

1 个答案:

答案 0 :(得分:1)

使用target时,您仍然可以访问fromEvent()。您只需输入ev.target.value

即可