angular - markAsUntouched 未清除红线

标签 angular angular-material2

我正在使用 Angular Material 的 react 形式。 当我单击 onsubmit() 时,我希望所有没有值的参数都不会显示错误(红线)

我尝试使用 this.formGroup.markAsUntouched(); 但它不起作用。

当我不在 Angular Material 上使用时,它可以工作。 有人知道如何正确使用 Angular Material 吗?

<form [formGroup]="formGroup" (ngSubmit)="onSubmit(formGroup.value)" class="form">
    <mat-form-field class="form-element">
        <input matInput placeholder="Emailaddress" formControlName="email">
    </mat-form-field>
</form>



onSubmit(post) {
    this.formGroup.markAsUntouched();
}

formGroup 将保持不变,但红线仍然出现。

最佳答案

formReset(formGroup: FormGroup) {
    formGroup.control.reset();
    formGroup.control.markAsPristine();
    formGroup.control.markAsUntouched();
}

// for safety measure try calling this function in a try-catch block, it works on a reactive forms

关于angular - markAsUntouched 未清除红线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203913/

相关文章:

angular - 如果出现 md-error,则聚焦 FormControl

angular - 获取 Angular Material 嵌套树中子节点的父节点

javascript - 过滤管道总是记录未定义的

angular - 如何在 Angular 中的一个元素上应用多个模板绑定(bind)

javascript - 使用 ngx-filter-pipe angular 过滤多个值

Angular2 或 4 Material TreeView

css - Angular Material - 全局颜色变量

css - Angular 2 Material无法加载样式

angular - 创建了新组件,但我在模块中收到有关提供程序的错误

javascript - 在调用函数之前设置类范围变量