- 表示实体状态的形式被编辑(变为脏)正在提交
- 的形式和实体状态现在与以下形式的状态,这意味着形式现在应该被设置为原始排列。
我们该怎么做? ng1中有$setPristine()
。 顺便说一句,我说的是ControlGroup
窗体的类型。如何将表单设置为原始状态?
我们该怎么做? ng1中有$setPristine()
。 顺便说一句,我说的是ControlGroup
窗体的类型。如何将表单设置为原始状态?
有markAsPristine
方法(它现在似乎没有记录,但可以在这里找到:https://github.com/angular/angular/blob/53f0c2206df6a5f8ee03d611a7563ca1a78cc82d/tools/public_api_guard/forms/index.d.ts#L42)。
基本上,this.form.markAsPristine()
做你的期望。
更新
在新的形式的模块这是提高了很多。
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/5568和https://github.com/angular/angular/issues/4933。通常的解决方法是重新创建表单以获得原始表单。
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版或更高版本会显示出来。
........... super ........... – Birowsky