Angular 2 change event on every keypress

后端 未结 11 1379
说谎
说谎 2020-11-27 09:36

The change event is only called after the focus of the input has changed. How can I make it so that the event fires on every keypress?



        
相关标签:
11条回答
  • 2020-11-27 10:12

    What you're looking for is

    <input type="text" [(ngModel)]="mymodel" (keyup)="valuechange()" />
    {{mymodel}}
    

    Then do whatever you want with the data by accessing the bound this.mymodel in your .ts file.

    0 讨论(0)
  • 2020-11-27 10:13

    The (keyup) event is your best bet.

    Let's see why:

    1. (change) like you mentioned triggers only when the input loses focus, hence is of limited use.
    2. (keypress) triggers on key presses but doesn't trigger on certain keystrokes like the backspace.
    3. (keydown) triggers every time a key is pushed down. Hence always lags by 1 character; as it gets the element state before the keystroke was registered.
    4. (keyup) is your best bet as it triggers every time a key push event has completed, hence this also includes the most recent character.

    So (keyup) is the safest to go with because it...

    • registers an event on every keystroke unlike (change) event
    • includes the keys that (keypress) ignores
    • has no lag unlike the (keydown) event
    0 讨论(0)
  • 2020-11-27 10:16

    Use ngModelChange by breaking up the [(x)] syntax into its two pieces, i.e., property databinding and event binding:

    <input type="text" [ngModel]="mymodel" (ngModelChange)="valuechange($event)" />
    {{mymodel}}
    
    valuechange(newValue) {
      mymodel = newValue;
      console.log(newValue)
    }
    

    It works for the backspace key too.

    0 讨论(0)
  • 2020-11-27 10:19

    A different way to handle such cases is to use formControl and subscribe to it's valueChanges when your component is initialized, which will allow you to use rxjs operators for advanced requirements like performing http requests, apply a debounce until user finish writing a sentence, take last value and omit previous, ...

    import {Component, OnInit} from '@angular/core';
    import { FormControl } from '@angular/forms';
    import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
    
    @Component({
      selector: 'some-selector',
      template: `
        <input type="text" [formControl]="searchControl" placeholder="search">
      `
    })
    export class SomeComponent implements OnInit {
      private searchControl: FormControl;
      private debounce: number = 400;
    
      ngOnInit() {
        this.searchControl = new FormControl('');
        this.searchControl.valueChanges
          .pipe(debounceTime(this.debounce), distinctUntilChanged())
          .subscribe(query => {
            console.log(query);
          });
      }
    }
    
    0 讨论(0)
  • 2020-11-27 10:21

    I just used the event input and it worked fine as follows:

    in .html file :

    <input type="text" class="form-control" (input)="onSearchChange($event.target.value)">
    

    in .ts file :

    onSearchChange(searchValue: string): void {  
      console.log(searchValue);
    }
    
    0 讨论(0)
提交回复
热议问题