2016-05-13 102 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版或更高版本會顯示出來。