如何将表单设置为原始?

Bir*_*sky 20 angular2-forms angular

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

我们怎么做?有$setPristine()在NG1.顺便说一句,我说的ControlGroup是形式.

Chr*_*dal 32

markAsPristine方法(它现在似乎没有记录,但可以在这里找到:https://github.com/angular/angular/blob/53f0c2206df6a5f8ee03d611a7563ca1a78cc82d/tools/public_api_guard/forms/index.d.ts#L42).

基本上,this.form.markAsPristine()做你期望的.


Gün*_*uer 11

更新

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

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

markAsTouched({onlySelf}?: {onlySelf?: boolean}) : void
markAsUntouched({onlySelf}?: {onlySelf?: boolean}) : void
markAsDirty({onlySelf}?: {onlySelf?: boolean}) : void
markAsPristine({onlySelf}?: {onlySelf?: boolean}) : void
markAsPending({onlySelf}?: {onlySelf?: boolean}) : void
Run Code Online (Sandbox Code Playgroud)

还有其他几种新方法

disable({onlySelf, emitEvent}?: {onlySelf?: boolean, emitEvent?: boolean}) : void
enable({onlySelf, emitEvent}?: {onlySelf?: boolean, emitEvent?: boolean}) : void
Run Code Online (Sandbox Code Playgroud)
setValue(value: any, options?: Object) : void
patchValue(value: any, options?: Object) : void
Run Code Online (Sandbox Code Playgroud)
reset(value?: any, options?: Object) : void
updateValueAndValidity({onlySelf, emitEvent}?: {onlySelf?: boolean,  emitEvent?: boolean}) : void // (old)
setErrors(errors: {[key: string]: any}, {emitEvent}?: {emitEvent?: boolean}) : void
Run Code Online (Sandbox Code Playgroud)

原版的

目前尚不支持.请参阅https://github.com/angular/angular/issues/5568https://github.com/angular/angular/issues/4933.通常的解决方法是重新创建表单以获得原始表单.