ngModel
如果输入字段的值已更改,我想在 Angular2 组件中运行一些代码。我想收听该(input)
事件,因为它是在任何类型的用户交互时触发的。这意味着我无法使用(change)
这里使用该事件。我只想在值发生变化时运行我的代码。
这是一个示例组件:
import { Component } from '@angular/core';
@Component({
selector: 'myCuteComponent',
templateUrl: `
<h1>yoO World</h1>
<input [(ngModel)]="cuteValue" (input)="onInput($event)">
`
})
export class MyCuteComponent {
cuteValue = '';
constructor() { }
onInput(event) {
if (event.target.value !== this.cuteValue) {
console.log('value has changed'); // this will never run :(
}
}
}
问题是它在被触发event.target.value
时已经包含了新值。onInput
所以这种方式行不通,console.log will
永远不会运行。
问题:是否有适当的(和通用的)解决方案来检测在任何类型的用户交互后价值是否真的发生了变化?