更改事件仅在输入的焦点发生更改后才被调用。我怎样才能使事件在每次按键时触发?

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

第二次绑定在每个按键上都发生变化。


当前回答

对于响应式表单,您可以订阅对所有字段或仅对特定字段所做的更改。

获取一个FormGroup的所有更改:

this.orderForm.valueChanges.subscribe(value => {
    console.dir(value);
});

获取特定字段的更改:

this.orderForm.get('orderPriority').valueChanges.subscribe(value => {
    console.log(value);
  });

其他回答

处理这种情况的另一种方法是使用formControl并在组件初始化时订阅它的valueChanges,这将允许您使用rxjs操作符来执行高级要求,如执行http请求,应用debounce直到用户写完一个句子,获取最后一个值并省略前一个值,等等。

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);
      });
  }
}

我只是使用事件输入,它工作得很好如下:

在.html文件中:

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

在.ts文件中:

onSearchChange(searchValue: string): void {  
  console.log(searchValue);
}

(按键)项目是你最好的选择。

让我们来看看原因:

(变化)就像你提到的,只有当输入失去焦点时才会触发,因此用处有限。 (按键)在按键时触发,但在某些按键(如退格键)时不触发。 (keydown)每次按下键时触发。因此总是滞后1个字符;因为它获得了注册击键之前的元素状态。 (keyup)是你最好的选择,因为它会在每次按键推送事件完成时触发,因此这也包括最近的字符。

所以(keyup)是最安全的,因为它…

与(change)事件不同,在每次击键时都注册一个事件 包括(按键)忽略的键 没有延迟不像(keydown)事件

保持angular ngModel同步的秘密事件是事件调用输入。因此,你的问题的最佳答案应该是:

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

我一直在使用keyup在一个数字字段,但今天我注意到在chrome的输入有上升/下降按钮来增加/减少的值,这是不被keyup识别。

我的解决方案是使用keyup和更改一起:

(keyup)="unitsChanged[i] = true" (change)="unitsChanged[i] = true"

初步测试表明,这工作良好,将在进一步测试后,如果发现任何错误。