问题描述
我知道Angular 2当前缺乏一种将表单轻松重置为原始状态的方法.随便看看,我发现了一种下面的解决方案,它可以重置表单字段.
I am aware that Angular 2 currently lacks a way to easily reset a form to a pristine state. Poking around I have found a solution like the one below that resets the form fields.
有人建议我需要删除控制组并创建一个新的控件组以将其重建为原始格式.我很难找出最好的方法来做到这一点.我知道我需要将表单构建包装在一个函数中,但是在构造函数中执行此操作时会遇到错误.
It has been suggested that I need to drop the control group and create a new one to rebuild the form as pristine. I am having difficulty figuring out the best way to do this. I know I need to wrap the form building within a function but I am running into errors when doing that within the constructor.
重建对照组以完全重置表格的最佳方法是什么?
What would be the best way to rebuild the control group to completely reset the form?
class App {
name: Control;
username: Control;
email: Control;
form: ControlGroup;
constructor(private builder: FormBuilder) {
this.name = new Control('', Validators.required);
this.email = new Control('', Validators.required);
this.username = new Control('', Validators.required);
this.form = builder.group({
name: this.name,
email: this.email,
username: this.username
});
}
onSubmit(value: any): void {
// code that happens when form is submitted
// then reset the form
this.reset();
}
reset() {
for (let name in this.form.controls) {
this.form.controls[name].updateValue('');
this.form.controls[name].setErrors(null);
}
}
}
推荐答案
> = RC.6
支持重置表格并保持submitted
状态.
Support resetting forms and maintain a submitted
state.
console.log(this.form.submitted);
this.form.reset()
或
this.form = new FormGroup()...;
导入更新
要将表单控件设置为创建表单时的状态,例如验证程序,则需要进行一些其他测量
To set the Form controls to a state when the form is created, like validators, some additional measurements are necessary
在表单(html)的视图部分中,添加*ngIf
以显示或隐藏表单
In the view part of the form (html) add an *ngIf
to show or hide the form
<form *ngIf="showForm"
在表单(* .ts)的组件侧执行此操作
In the component side of the form (*.ts) do this
showForm:boolean = true;
onSubmit(value:any):void {
this.showForm = false;
setTimeout(() => {
this.reset()
this.showForm = true;
});
}
这是一个更详细的示例:
Here is a more detailed example:
export class CreateParkingComponent implements OnInit {
createParkingForm: FormGroup ;
showForm = true ;
constructor(
private formBuilder: FormBuilder,
private parkingService: ParkingService,
private snackBar: MatSnackBar) {
this.prepareForm() ;
}
prepareForm() {
this.createParkingForm = this.formBuilder.group({
'name': ['', Validators.compose([Validators.required, Validators.minLength(5)])],
'company': ['', Validators.minLength(5)],
'city': ['', Validators.required],
'address': ['', Validators.compose([Validators.required, Validators.minLength(10)])],
'latitude': [''],
'longitude': [''],
'phone': ['', Validators.compose([Validators.required, Validators.minLength(7)])],
'pictureUrl': [''],
// process the 3 input values of the maxCapacity'
'pricingText': ['', Validators.compose([Validators.required, Validators.minLength(10)])],
'ceilingType': ['', Validators.required],
});
}
ngOnInit() {
}
resetForm(form: FormGroup) {
this.prepareForm();
}
createParkingSubmit() {
// Hide the form while the submit is done
this.showForm = false ;
// In this case call the backend and react to the success or fail answer
this.parkingService.create(p).subscribe(
response => {
console.log(response);
this.snackBar.open('Parqueadero creado', 'X', {duration: 3000});
setTimeout(() => {
//reset the form and show it again
this.prepareForm();
this.showForm = true;
});
}
, error => {
console.log(error);
this.showForm = true ;
this.snackBar.open('ERROR: al crear Parqueadero:' + error.message);
}
);
}
}
原始< = RC.5 只需将创建表单的代码移到方法上,并在处理了提交之后再次调用它即可:
original <= RC.5Just move the code that creates the form to a method and call it again after you handled submit:
@Component({
selector: 'form-component',
template: `
<form (ngSubmit)="onSubmit($event)" [ngFormModel]="form">
<input type="test" ngControl="name">
<input type="test" ngControl="email">
<input type="test" ngControl="username">
<button type="submit">submit</button>
</form>
<div>name: {{name.value}}</div>
<div>email: {{email.value}}</div>
<div>username: {{username.value}}</div>
`
})
class FormComponent {
name:Control;
username:Control;
email:Control;
form:ControlGroup;
constructor(private builder:FormBuilder) {
this.createForm();
}
createForm() {
this.name = new Control('', Validators.required);
this.email = new Control('', Validators.required);
this.username = new Control('', Validators.required);
this.form = this.builder.group({
name: this.name,
email: this.email,
username: this.username
});
}
onSubmit(value:any):void {
// code that happens when form is submitted
// then reset the form
this.reset();
}
reset() {
this.createForm();
}
}
这篇关于提交后在Angular 2中重置表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!