Angular2 Rxjs keyup 如何连接字符
Angular2 Rxjs keyup how to concat characters
我的目标是连接字符
像这样输入
<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);
您在使用 fromEvent()
时仍然可以访问 target
。您只需输入 ev.target.value
.
我的目标是连接字符 像这样输入
<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);
您在使用 fromEvent()
时仍然可以访问 target
。您只需输入 ev.target.value
.