我有一些简单的角度2组件与模板.提交后如何清除表格和所有字段?我无法重新加载页面.用date.setValue('')
字段设置数据后是stil touched
.
import {Component} from 'angular2/core';
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, Control} from 'angular2/common';
@Component({
selector: 'loading-form',
templateUrl: 'app/loadings/loading-form.component.html',
directives: [FORM_DIRECTIVES]
})
export class LoadingFormComponent {
private form:ControlGroup;
private date:Control;
private capacity:Control;
constructor(private _loadingsService:LoadingsService, fb:FormBuilder) {
this.date = new Control('', Validators.required);
this.capacity = new Control('', Validators.required);
this.form = fb.group({
'date': this.date,
'capacity': this.capacity
});
}
onSubmit(value:any):void {
//send some data to backend
}
}
装载-form.component.html
Loading form
Günter Zöchb.. 106
另请参阅https://angular.io/docs/ts/latest/guide/reactive-forms.html("重置表单标志"部分)
> = RC.6
在RC.6中,应该支持更新表单模型.创建新表单组并分配给myForm
[formGroup]="myForm"
也将得到支持(https://github.com/angular/angular/pull/11051#issuecomment-243483654)
> = RC.5
form.reset();
在新表单模块(> = RC.5)中NgForm
有一个reset()
方法,并且还支持表单reset
事件.
https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179
<= RC.3
这将有效:
onSubmit(value:any):void {
//send some data to backend
for(var name in form.controls) {
(form.controls[name]).updateValue('');
/*(form.controls[name]).updateValue('');*/ this should work in RC4 if `Control` is not working, working same in my case
form.controls[name].setErrors(null);
}
}
也可以看看
https://github.com/angular/angular/issues/6196
https://github.com/angular/angular/issues/6169
https://github.com/angular/angular/issues/4933
https://github.com/angular/angular/issues/4914
https://github.com/angular/angular/issues/6371
@ masel.popowo是的,如果您想使用“ pristine`,...”,那么重建表格是当前的唯一选择。 (2认同)
ebhh2001.. 25
从Angular2 RC5开始,myFormGroup.reset()
似乎就是这样做的.
另请参阅https://angular.io/docs/ts/latest/guide/reactive-forms.html("重置表单标志"部分)
> = RC.6
在RC.6中,应该支持更新表单模型.创建新表单组并分配给myForm
[formGroup]="myForm"
也将得到支持(https://github.com/angular/angular/pull/11051#issuecomment-243483654)
> = RC.5
form.reset();
在新表单模块(> = RC.5)中NgForm
有一个reset()
方法,并且还支持表单reset
事件.
https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179
<= RC.3
这将有效:
onSubmit(value:any):void {
//send some data to backend
for(var name in form.controls) {
(form.controls[name]).updateValue('');
/*(form.controls[name]).updateValue('');*/ this should work in RC4 if `Control` is not working, working same in my case
form.controls[name].setErrors(null);
}
}
也可以看看
https://github.com/angular/angular/issues/6196
https://github.com/angular/angular/issues/6169
https://github.com/angular/angular/issues/4933
https://github.com/angular/angular/issues/4914
https://github.com/angular/angular/issues/6371
从Angular2 RC5开始,myFormGroup.reset()
似乎就是这样做的.
要在提交后重置表单,只需调用即可this.form.reset()
.通过调用reset()
它将:
将控件和子控件标记为原始.
将控件和子控件标记为未触及.
将控件和子控件的值设置为自定义值或null.
更新受影响方的价值/有效期/错误.
请找到此拉取请求以获得详细答案.仅供参考,此PR已合并至2.0.0.
希望这可以有所帮助,如果您对Angular2表单有任何其他问题,请告诉我.
import {Component} from '@angular/core'; import {NgForm} from '@angular/forms'; @Component({ selector: 'example-app', template: '
First name value: {{ first.value }}
First name valid: {{ first.valid }}
Form value: {{ f.value | json }}
Form valid: {{ f.valid }}
', }) export class SimpleFormComp { onSubmit(f: NgForm) { // some stuff f.resetForm(); } }clearForm();
在您的.ts文件中拨打电话
尝试像下面的示例代码片段一样清除表单数据。
clearForm() { this.addContactForm.reset({ 'first_name': '', 'last_name': '', 'mobile': '', 'address': '', 'city': '', 'state': '', 'country': '', 'zip': '' }); }
对于角度版本4,您可以使用:
this.heroForm.reset();
但是,您可能需要一个初始值,如:
ngOnChanges() { this.heroForm.reset({ name: this.hero.name, //Or '' to empty initial value. address: this.hero.addresses[0] || new Address() }); }
在对象引用中解决null问题很重要.
参考链接,搜索"重置表单标志".