我意识到,当尝试为 mat-radio-group 设置验证错误消息时,用于其他元素的规则在这种情况下不太适合,因为一旦选择它就不会变得脏或触摸,但如果我只设置无效和原始,那么错误将从头开始显示,这不是我们想要的。尝试提交整个表单时,唯一的乳清是激活错误消息的,如果是,该怎么做?
<mat-radio-group id="gender" formControlName="gender" class="example-full-width" class="example-radio-group">
<mat-radio-button class="example-radio-button" *ngFor="let gender of genders" [value]="gender" required>
{{gender}}
</mat-radio-button>
</mat-radio-group>
<div *ngIf="registerForm.controls.gender.invalid && (registerForm.controls.gender.dirty || registerForm.controls.gender.touched)"
class="alert alert-danger">
<div class="error mat-body-2" *ngIf="registerForm.controls.gender.errors.required">
You must select your gender
</div>
</div>