改变:
当用户提交对元素值的更改时,将针对 、
- 句法:
- 例子:
HTML文件:
打字稿文件:
const selectElement = document.querySelector('.cartoon');
selectElement.addEventListener('change', (event) => {
const result = document.querySelector('.result');
result.textContent = `You like ${event.target.value}`;
});
- 输出
ngModelChange:
当用户想要更改模型时,通过在输入中输入文本,事件回调将触发并将新值设置为模型。我们不能在没有 ngModel 的情况下使用 mgModelChange,因为 ngModel 类具有带有 EventEmitter 实例的更新函数。 ngModelChange 只会在模型更改或更新时触发。
- 句法
- 例子
HTML文件:
Please check your ranges
- 打字稿文件:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
public greaterThanValue = 0;
public lessThanValue = 1;
public isInvalid: boolean = false;
public onChange(event: any): void {
this.isInvalid =
this.greaterThanValue > this.lessThanValue;
}
}
- 输出
区别:
change | ngModelChange |
---|---|
change is bound to the HTML onchange event. It is a DOM event. | ngModelChange is bound to the model variable binded to your input. |
No such class is required. | ngModelChange need ngModel class to function. |
change event bound to classical input change event. | ngModelChange It fires when the model changes. You cannot use this event without ngModel directive. |
change triggers when the user changes the input. | ngModelChange triggers when the model changes, irrespective of that change is caused by the user or not. |