在Angular中,可以使用禁用属性和禁用状态来禁用表单控件,并稍后读取其值。
以下是一个示例解决方法:
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
myValue: string;
isDisabled: boolean = true;
enableControl() {
this.isDisabled = false;
}
disableControl() {
this.isDisabled = true;
}
}
在上面的代码中,isDisabled
变量用于控制是否禁用输入框。当点击“启用”按钮时,调用enableControl()
方法将isDisabled
设置为false
,从而启用输入框。当点击“禁用”按钮时,调用disableControl()
方法将isDisabled
设置为true
,从而禁用输入框。
通过这种方式,您可以在稍后读取表单控件的值,并且可以根据需要禁用或启用它。