Angular 使用有效控件的表单无效
Angular form invalid with valid controls
我正在创建一个具有现有值的组:
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 中吗?以便我们能够理解您的确切意思?
我正在创建一个具有现有值的组:
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 中吗?以便我们能够理解您的确切意思?