带有有效控件的角度形式无效



我正在用现有值创建一个from组:

this.form = new FormGroup({
field1: new FormControl(
{
value: obj ? obj.value : null,
disabled: obj
},
Validators.required
),
field2: new FormControl(
{
value: obj ? obj.value : null,
disabled: obj
},
Validators.required
)
});

复制:https://stackblitz.com/edit/angular-aadkss

我希望它在obj存在时填充值,有效且禁用,但它正在填充值,禁用且无效,无效。如何使其有效?

这是不可能的,.valid.disabled的值取决于控件的.status的值,后者只能有一个值,请参阅https://github.com/angular/angular/blob/f27deea003822ed46437e74f102b281f692b7811/packages/forms/src/model.ts#L191

/**
* The validation status of the control. There are four possible
* validation status values:
*
* * **VALID**: This control has passed all validation checks.
* * **INVALID**: This control has failed at least one validation check.
* * **PENDING**: This control is in the midst of conducting a validation check.
* * **DISABLED**: This control is exempt from validation checks.
*
* These status values are mutually exclusive, so a control cannot be
* both valid AND invalid or invalid AND disabled.
*/
public readonly status!: string;

你能把它放进一个stackblitz中吗?这样我们就能理解你的意思了?

相关内容

最新更新