我有一些简单的角度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
<div class="card card-block">
<h3 class="card-title">Loading form</h3>
<form (ngSubmit)="onSubmit(form.value)" [ngFormModel]="form">
<fieldset class="form-group" [class.has-danger]="!date.valid && date.touched">
<label class="form-control-label" for="dateInput">Date</label>
<input type="text" class="form-control form-control-danger form-control-success" id="dateInput"
min="0" placeholder="Enter loading date"
[ngFormControl]="form.controls['date']">
</fieldset>
<fieldset class="form-group" [class.has-danger]="!capacity.valid && capacity.touched">
<label class="form-control-label" for="capacityInput">Capacity</label>
<input type="number" class="form-control form-control-danger form-control-success" id="capacityInput"
placeholder="Enter capacity"
[ngFormControl]="form.controls['capacity']">
</fieldset>
<button type="submit" class="btn btn-primary" [disabled]="!form.valid">Submit
</button>
</form>
</div>
10 回答
另见https://angular.io/docs/ts/latest/guide/reactive-forms.html(第"reset the form flags"节)
>=RC.6
在RC.6中,应该支持更新表单模型 . 创建新表单组并分配给
myForm
也将得到支持(https://github.com/angular/angular/pull/11051#issuecomment-243483654)
>=RC.5
在新表单模块(> = RC.5)中,
NgForm
具有reset()
方法,并且还支持表单reset
事件 . https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179<=RC.3
这将有效:
也可以看看
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()
它将:将控件和子控件标记为 pristine .
将控件和子控件标记为 untouched .
将控件和子控件的值设置为 custom value 或 null .
更新 value/validity/errors 受影响的各方 .
请找到pull request以获得详细答案 . 仅供参考,此PR已合并至2.0.0 .
希望这可以有所帮助,如果您对Angular2表单有任何其他问题,请告诉我 .
对于角度版本4,您可以使用:
但是,您可能需要一个初始值,如:
在对象引用中解决null问题很重要 .
参考link,搜索"reset the form flags" .
有一个新的讨论(https://github.com/angular/angular/issues/4933) . 到目前为止,只有一些黑客可以清除表单,比如在提交后重新创建整个表单:https://embed.plnkr.co/kMPjjJ1TWuYGVNlnQXrU/
在.ts文件中拨打电话
clearForm();
尝试使用下面的示例代码段来清除表单数据 .
我发现了另一个解决方它有点hacky但它在angular2世界广泛使用 .
由于* ngIf指令删除了表单并重新创建它,因此可以简单地向表单添加* ngIf并将其绑定到某种
formSuccessfullySent
变量 . =>这将重新创建表单,从而重置输入控件状态 .当然,您还必须清除模型变量 . 我发现为表单字段设置特定的模型类很方便 . 这样我可以重置所有字段,就像创建此模型类的新实例一样简单 . :)
嗯,现在(2017年1月23日,角度为2.4.3)我的工作方式如下: