【发布时间】:2018-06-10 03:25:39
【问题描述】:
我在组件的模板上有一个表单:
<form (ngSubmit)="submitLogin(loginForm)" #loginForm="ngForm">
<mat-input-container>
<input matInput [placeholder]="'User Name'" ngModel name="username" autofocus required>
</mat-input-container>
<br>
<mat-input-container>
<input matInput [placeholder]="'Password'" ngModel type="password" name="password" required>
</mat-input-container>
<br>
<button [disabled]="loginForm.invalid" mat-raised-button type="submit">
Login
</button>
</form>
这是我的组件的提交处理程序:
public submitLogin(loginForm: NgForm) {
return this.authService.login(loginForm.value)
.subscribe(
res => this.router.navigate(['/customers']),
error => loginForm.controls.password.reset() // this place!
);
}
我看到了它的工作原理和登录错误(传递了一些随机值)
问题。如何重置表单上的确切字段并使其真正原始且未受影响?所以它应该是有效的,而不用在 UI 上用红色的“无效”边框标记它。
在loginForm.controls.password.reset() 之后,我看到loginForm.controls.password.touched 是false,loginForm.controls.password.pristine 是true,但我也看到loginForm.controls.password.status 是“无效”。如果我破解它并直接将“VALID”值分配给status 属性,红色无效边框会消失,但如果我专注于该字段然后消失,它会破坏失去焦点的失效没有任何输入。应该有一种合法的方式来重置提交的表单并使其同时生效。
【问题讨论】:
标签: angular angular-material angular-forms