2016-05-13 98 views
12
  • 表示实体状态的形式被编辑(变为脏)正在提交
  • 的形式和实体状态现在与以下形式的状态,这意味着形式现在应该被设置为原始排列。

我们该怎么做? ng1中有$setPristine()。 顺便说一句,我说的是ControlGroup窗体的类型。如何将表单设置为原始状态?

回答

8

更新

在新的形式的模块这是提高了很多。

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 

和其他一些新的方法

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

原始

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

+0

........... super ........... – Birowsky

0
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 
    } 

https://github.com/angular/angular/pull/9974

在RC5版或更高版本会显示出来。