Angular 如何将窗体设置为原始? 正在编辑表示实体状态的表单(变脏) 表单正在提交,实体状态现在与表单状态对齐,这意味着表单现在应该设置为原始状态

Angular 如何将窗体设置为原始? 正在编辑表示实体状态的表单(变脏) 表单正在提交,实体状态现在与表单状态对齐,这意味着表单现在应该设置为原始状态,angular,angular2-forms,Angular,Angular2 Forms,我们怎么做? ng1中存在$setPristine()。 顺便说一句,我说的是ControlGroup表单的类型。更新 在新的表单模块中,这得到了很大的改进 ,大多数表单类的基类提供 markAsTouched({onlySelf}?:{onlySelf?:boolean}):void markAsUntouched({onlySelf}?:{onlySelf?:boolean}):void markAsDirty({onlySelf}?:{onlySelf?:boolean}):无效 mark

我们怎么做? ng1中存在
$setPristine()

顺便说一句,我说的是
ControlGroup
表单的类型。

更新

在新的表单模块中,这得到了很大的改进

,大多数表单类的基类提供

markAsTouched({onlySelf}?:{onlySelf?:boolean}):void
markAsUntouched({onlySelf}?:{onlySelf?:boolean}):void
markAsDirty({onlySelf}?:{onlySelf?:boolean}):无效
markAsPristine({onlySelf}?:{onlySelf?:boolean}):无效
markAsPending({onlySelf}?:{onlySelf?:boolean}):void
以及其他一些新方法

disable({onlySelf,emitEvent}?:{onlySelf?:boolean,emitEvent?:boolean}):void
启用({onlySelf,emitEvent}?:{onlySelf?:boolean,emitEvent?:boolean}):无效
setValue(值:任意,选项?:对象):无效
patchValue(值:任意,选项?:对象):无效
reset(值?:任何,选项?:对象):无效
updateValueAndValidity({onlySelf,emitEvent}?:{onlySelf?:boolean,emitEvent?:boolean}):void//(旧)
setErrors(错误:{[key:string]:any},{emitEvent}?:{emitEvent?:boolean}):无效
原创

这目前不受支持。见和。通常的解决方法是重新创建表单以获得原始表单

class MyComp {
   form = new FormGroup({
      first: new FormControl('Nancy'),
      last: new FormControl('Drew')
   });
}

   reset() {
      this.form.reset();  // will reset to null
     // this.form.reset({first: 'Nancy', last: 'Drew'});   -- will reset to value specified
   }


这将在rc5或更高版本中显示。

markAsPristine
方法(目前似乎没有文档记录,但可以在此处找到:)

基本上,
this.form.markAsPristine()
会满足您的期望