在Angular中,"fieldset disabled"和"formgroup.disable"都可以用来实现表单的只读状态,但它们之间有一些区别。
import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-my-form',
template: `
`,
})
export class MyFormComponent {
myForm: FormGroup;
constructor() {
this.myForm = new FormGroup({
name: new FormControl(),
email: new FormControl(),
});
this.myForm.disable(); // 禁用表单
}
}
无论是使用"fieldset disabled"还是"formgroup.disable",都可以实现表单的只读状态。但是,如果你想在Angular中进行更多的表单验证和处理逻辑,使用"formgroup.disable"更为灵活和方便。