代码之家  ›  专栏  ›  技术社区  ›  Whisher

Angular2 Rxjs键控如何连接字符

  •  0
  • Whisher  · 技术社区  · 9 年前

    像这样输入

    <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);
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Meligy    9 年前

    您仍然可以访问 target 使用时 fromEvent() .你可以直接输入 ev.target.value .

    推荐文章