gpt4 book ai didi

angular - 提交后在 Angular 2 中重置表单

转载 作者:太空狗 更新时间:2023-10-29 16:46:08 25 4
gpt4 key购买 nike

我知道 Angular 2 目前缺少一种方法可以轻松地将表单重置为原始状态。四处寻找,我找到了一种解决方案,如下所示,可以重置表单字段。

有人建议我需要删除控制组并创建一个新组以将表单重建为原始状态。我很难找出最好的方法来做到这一点。我知道我需要将表单构建包装在一个函数中,但是在构造函数中这样做时我遇到了错误。

重建控制组以完全重置表单的最佳方法是什么?

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

支持重置表单并保持已提交状态。

console.log(this.form.submitted);
this.form.reset()

this.form = new FormGroup()...;

导入更新

要在创建表单时将表单控件设置为状态,如验证器,需要一些额外的测量

在表单的 View 部分(html)添加一个*ngIf来显示或隐藏表单

<form *ngIf="showForm"

在表单的组件端 (*.ts) 执行此操作

  showForm:boolean = true;

onSubmit(value:any):void {
this.showForm = false;
setTimeout(() => {
this.reset()
this.showForm = true;
});
}

这是一个更详细的例子:

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);
}
);
}
}

Plunker example

原始 <= RC.5只需将创建表单的代码移动到一个方法中,并在处理提交后再次调用它:

@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();
}
}

Plunker example

关于angular - 提交后在 Angular 2 中重置表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com