编辑:简单回答
你可以打电话
this.form.get('username').markAsTouched();
另一个答案
如果将按钮放在表单中,则添加
(ngSubmit)="USR($event)"
type="submit"
在您的按钮上,您将进行角度检测,如果形式是有效的或不是,然后将显示错误消息。
<form class="example-form" [formGroup]="form" (ngSubmit)=USR($event)>
<mat-form-field class="example-full-width">
<input matInput placeholder="Username" formControlName="username">
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Password" formControlName="password">
</mat-form-field>
<button mat-raised-button type="button" color="primary" [style.margin]="'12px'" (click)="PWD()">Update password with value</button>
<button type="submit" mat-raised-button color="primary" [style.margin]="'12px'">Update username required error</button>
</form>
Here is a working example