【问题标题】:Showing red color for required field controls after calling form.reset() in angular material在角度材料中调用 form.reset() 后,必填字段控件显示为红色
【发布时间】:2018-04-04 20:36:11
【问题描述】:

Attached screenshot for more information about error

我正在使用 FormGroup 来创建表单控件,并且在将填充的数据提交到服务器后,我正在调用 form.reset() 方法来清除当前数据。 所以我想要的是,所有控件都应该像组件第一次初始化时一样呈现

html文件代码:

<mat-form-field>
     <input matInput id="title" placeholder="Enter your title" 
     formControlName="title">
</mat-form-field>

TS 文件代码:

title: new FormControl("", Validators.compose([
        Validators.required,
      ])),

【问题讨论】:

  • 你能提供更多代码吗
  • 点击提交功能时可以使用$scope.form={}重置输入字段
  • $scope.form = {} 在 Angular 4 中可用吗?

标签: angularjs angular-forms


【解决方案1】:

这是angular5

  <form [formGroup]="myform" novalidate (ngSubmit)="onSubmit()">
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>

js

onSubmit()
{
if(this.myform.valid)
{
console.log("form submitted",this.myform.value)
this.myform.reset();
}
}

查看此链接了解更多check the link

【讨论】:

  • 没有。表单重置成功,但必填字段控制下的红线仍然存在(附在上面的屏幕截图中)
  • 除了form.reset()方法还有什么方法可以清除Material表单的输入字段值
  • 可以通过$scope.form.name清除输入字段值
  • 显示 $scope 的语法错误。我应该使用一个带有表单参数的类,并在提交后用空值初始化该类的每个参数吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多