像这样输入
<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)
更新2(见@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);