Html Mattabs模块, ... ] });
如果您只想在一个表单上使用此ErrorStateMatcher,这是可能的。请看。这是相同的原理。您是否可以尝试调用Html Mattabs模块, ... ] });,html,angular,typescript,angular-material,angular-reactive-forms,Html,Angular,Typescript,Angular Material,Angular Reactive Forms,如果您只想在一个表单上使用此ErrorStateMatcher,这是可能的。请看。这是相同的原理。您是否可以尝试调用This.myForm.markAsUntouched()?根据文档,该代码无效且不必要。()这个答案是否回答了你的问题?这个问题真的需要解决,就像你说的,使用这个解决方法很不方便。。。就我而言,我有一个非常独特的情况,我需要在不清除表单值(resetForm())的情况下重置“submitted”(已提交),这是对我所做的。为了避免这个问题,我做了(formDirective).
This.myForm.markAsUntouched()代码>?根据文档,该代码无效且不必要。()这个答案是否回答了你的问题?这个问题真的需要解决,就像你说的,使用这个解决方法很不方便。。。就我而言,我有一个非常独特的情况,我需要在不清除表单值(resetForm())的情况下重置“submitted”(已提交),这是对我所做的。为了避免这个问题,我做了(formDirective).submitted=false代码>。这是一种肮脏的黑客行为,但从源代码来看,没有明显的理由表明提交的字体脚本定义必须是只读的。非常蹩脚,我希望谷歌员工能做得更好。使用angular 7.2.2:NgForm类型的参数不可分配给FormGroupDirective类型
,在使用许多解决方案进行测试后,这一个非常有效!!这将只是删除验证程序,而不是重置它们。要清除验证程序,请使用.loginform.clearValidators(),然后将控件的错误设置为Null,这将与Angular 9完美配合。还有其他问题,这是正确的解决方案。从这条线索看,没有别的办法。我认为这是一个更好的答案。标记为答案的代码需要将局部变量传递给后面的代码,这是不可取的。但这个答案提供了有关表单如何工作的非常好的信息。对于Angular 8,“@ViewChild指令采用两个参数。除了字符串“formDirective”之外,另一个参数是元数据属性。有关更多信息,请参阅。答案很好,但他不仅重置了验证器,还重置了表单中的所有值(由用户输入)。这是在Angular 7+Material中对我有效的唯一答案。花了一整天的时间后,我找到了这个答案。setTimeout()有助于解决此问题。如果你能添加一些关于这个“黑客”是如何工作的描述,将会很有帮助。使用angular 7.2.8进行测试,虽然此代码可以回答问题,但提供有关如何和/或为什么解决问题的附加上下文将提高答案的长期价值。为我工作。我喜欢这个解决方案,因为它是最小的。
<form [formGroup]="myForm" (ngSubmit)="submitForm(myForm)">
<mat-form-field>
<input matInput formControlName="email" />
<mat-error *ngIf="email.hasError('required')">
Email is a required feild
</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="password" formControlName="password" />
<mat-error *ngIf="password.hasError('required')">
Password is a required feild
</mat-error>
</mat-form-field>
<button type="submit">Login</button>
</form>
export class MyComponent {
private myForm: FormGroup;
private email: FormControl = new FormContorl('', Validators.required);
private password: FormControl = new FormControl('', Validators.required);
constructor(
private formBuilder: FormBuilder
) {
this.myForm = formBuilder.group({
email: this.email,
password: this.password
});
}
private submitForm(formData: any): void {
this.myForm.reset();
}
}
<form [formGroup]="myForm" #formDirective="ngForm"
(ngSubmit)="submitForm(myForm, formDirective)">
private submitForm(formData: any, formDirective: FormGroupDirective): void {
formDirective.resetForm();
this.myForm.reset();
}
<form [formGroup]="createForm">
<button (click)="submitForm()" type="submit">Submit</button>
<button (click)="createForm.reset()" type="reset">Reset</button>
</form>
// you can put this method in a module and reuse it as needed
resetForm(form: FormGroup) {
form.reset();
Object.keys(form.controls).forEach(key => {
form.get(key).setErrors(null) ;
});
}
<form
...
#formDirective="ngForm"
>
import { ViewChild, ... } from '@angular/core';
import { NgForm, ... } from '@angular/forms';
export class MyComponent {
...
@ViewChild('formDirective') private formDirective: NgForm;
constructor(... )
private someFunction(): void {
...
formDirective.resetForm();
}
}
<form [formGroup]="myFormGroup" #myForm="ngForm">
…
<button mat-raised-button (click)="submitForm()">
</form>
submitForm() {
…
setTimeout(() => {
this.myForm.resetForm();
this.myFormGroup.reset();
}, 0);
}
this.myForm.get('formCtrlName').reset();
this.myForm.get('formCtrlName').setValidators([Validators.required, Validators.maxLength(45), Validators.minLength(4), Validators.pattern(environment.USER_NAME_REGEX)]);
this.myForm.get('formCtrlName').updateValueAndValidity();
<form [formGroup]="group" (ngSubmit)="onSubmit($event)">
<!-- your form here -->
</form>
public onSubmit(event): void {
// your code here
event.currentTarget.reset()
this.group.reset()
}
<form [formGroup]="MyForm" (ngSubmit)="submitForm()">
<input formControlName="Name">
<mat-error>
<span *ngIf="!tunersForm.get('Name').value && tunersForm.get('Name').touched"></span>
</mat-error>
<button type="reset" [disabled]="!MyForm.valid" (click)="submitForm()">Save</button>
</form>
this.registerForm.setErrors(null);
this.registerForm.valid
this.registerForm.get('email').setErrors(null)
resetForm() {
this.myFormGroup.reset();
this.myFormGroup.controls.food.setErrors(null);
this.myFormGroup.updateValueAndValidity();
}
@ViewChild(FormGroupDirective) formGroupDirective: FormGroupDirective;
this.formGroupDirective.resetForm();
this.form.reset();
for (let control in this.form.controls) {
this.form.controls[control].setErrors(null);
}
for (let control in this.form.controls) {
this.form.controls[control].setErrors(null);
}