单选按钮清除表单重置时的默认值 angular 6
Radio button clears default values on form reset angular 6
我有一个反应式表单,其中有一个单选按钮组,它对一个表单变量起作用,并且在我重置表单之前一直有效。当我重置表单时,两个收音机都被取消选择。
<mat-radio-group aria-label="Select an option" formControlName="IsUnderLc">
<mat-radio-button value="true" [checked]="workTransactionForm.controls['IsUnderLc'].value == true">
Under LC
</mat-radio-button>
<mat-radio-button value="false[checked]="workTransactionForm.controls['IsUnderLc'].value == false">
Collection
</mat-radio-button>
</mat-radio-group>
我想保留一个选择,我什至尝试修补值并在 FormGroup 中提供默认值,但没有任何效果。
this.workTransactionForm.reset();
this.workTransactionForm.controls.IsUnderLc.setValue(true);
有没有什么方法可以在重置表单后为表单控件设置值?
您可以在重置函数中为表单控件传递(新)值。这将重置表单和 keep/re-add 您传递的值。
所以你可以使用:
this.workTransactionForm.reset({IsUnderLc: true});
或者如果您想保留一些值:
const currentValue = this.workTransactionForm.get('IsUnderLc').value;
this.workTransactionForm.reset({IsUnderLc: currentValue});
我可以在更改 html 代码后解决问题
来自
<mat-radio-button value="false" [checked]="workTransactionForm.controls['IsUnderLc'].value == false">
Collection
</mat-radio-button>
到
<mat-radio-button value="false>
Collection
</mat-radio-button>
并在 .ts 文件中再次绑定它,例如
this.workTransactionForm.controls.IsFresh.patchValue(false);
this.workTransactionForm.controls.IsUnderLc.patchValue(false);
我有一个反应式表单,其中有一个单选按钮组,它对一个表单变量起作用,并且在我重置表单之前一直有效。当我重置表单时,两个收音机都被取消选择。
<mat-radio-group aria-label="Select an option" formControlName="IsUnderLc">
<mat-radio-button value="true" [checked]="workTransactionForm.controls['IsUnderLc'].value == true">
Under LC
</mat-radio-button>
<mat-radio-button value="false[checked]="workTransactionForm.controls['IsUnderLc'].value == false">
Collection
</mat-radio-button>
</mat-radio-group>
我想保留一个选择,我什至尝试修补值并在 FormGroup 中提供默认值,但没有任何效果。
this.workTransactionForm.reset();
this.workTransactionForm.controls.IsUnderLc.setValue(true);
有没有什么方法可以在重置表单后为表单控件设置值?
您可以在重置函数中为表单控件传递(新)值。这将重置表单和 keep/re-add 您传递的值。 所以你可以使用:
this.workTransactionForm.reset({IsUnderLc: true});
或者如果您想保留一些值:
const currentValue = this.workTransactionForm.get('IsUnderLc').value;
this.workTransactionForm.reset({IsUnderLc: currentValue});
我可以在更改 html 代码后解决问题 来自
<mat-radio-button value="false" [checked]="workTransactionForm.controls['IsUnderLc'].value == false">
Collection
</mat-radio-button>
到
<mat-radio-button value="false>
Collection
</mat-radio-button>
并在 .ts 文件中再次绑定它,例如
this.workTransactionForm.controls.IsFresh.patchValue(false);
this.workTransactionForm.controls.IsUnderLc.patchValue(false);