在Angular中,可以使用ngModel指令以及表单控件的value属性来实现将输入值重置为先前的值的功能。以下是一个示例解决方法:
previousValue: string;
resetValue(input: HTMLInputElement) {
input.value = this.previousValue;
}
完整的组件示例代码如下:
import { Component } from '@angular/core';
@Component({
selector: 'app-reset-value',
template: `
`
})
export class ResetValueComponent {
previousValue: string;
resetValue(input: HTMLInputElement) {
input.value = this.previousValue;
}
}
在上述示例中,通过ngModel指令将输入框的值与组件类中的previousValue属性进行双向绑定。当点击重置按钮时,调用resetValue方法将先前的值重新赋给输入框的值,从而实现将输入值重置为先前的值的功能。