如何在ng2中重置表单,我已经使用方法form.reset(),但在重置后我仍然检查了表单上的复选框。问题是,我如何取消选中表格中的复选框?
<form #f="ngForm" [formGroup]="taskCreate" (submit)="onSubmit(taskCreate)">
<fieldset class="form-group">
<label for="goal">Cel: </label>
<select name="goal" class="form-control" formControlName="goal" ngControl="goal">
<option *ngFor="let goal of goals" [value]="goal.key" [selected]="key === 'first'">{{ goal.value }}</option>
</select>
<div class="alert alert-danger" *ngIf="this.formValid.goalErr">You must select a goal.</div>
</fieldset>
<fieldset class="form-group">
<label for="p">Priorytet:</label>
<div name="p">
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PML" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.minustwo" alt="minustwo" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PL" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.minusone" alt="minusone" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PN" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.zero" alt="zero" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PH" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.plusone" alt="plusone" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PMH" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.plustwo" alt="plustwo" />
</div>
<div class="alert alert-danger" *ngIf="this.formValid.priorityErr">You must select a priority.</div>
</fieldset>
<fieldset class="form-group">
<label for="note">Uwagi do zadania:</label>
<textarea maxlength="2000" class="form-control" name="note" rows="8" cols="40" formControlName="note"></textarea>
<div class="alert alert-danger" *ngIf="this.formValid.noteErr">You must draw a note.</div>
</fieldset>
[...]
TS:
constructor(private appService: AppService, public cartBox: CartBox, public attachments: AttachmentList, private elementRef: ElementRef, private renderer: Renderer) {
super();
this.taskCreate = new FormGroup({
goal: new FormControl("", Validators.required),
prodCodeList: new FormControl("", Validators.required),
note: new FormControl("", Validators.required),
priority: new FormControl("", Validators.required),
attachmentList: new FormControl("")
});
问候! Bosper
答案 0 :(得分:3)
您可能需要重置每个表单控件。
form.reset()可以包含由所有formcontrols组成的对象。由于您尚未发布使用form.reset的方式,请尝试以下操作。
例如,尝试类似:
this.taskCreate.reset({
goal: '',
prodCodeList: '',
note: '',
priority: '',
attachmentList: ''
})
这应该重置你拥有的每个表单控件,这意味着它们将不受影响和原始。
文档here。
答案 1 :(得分:0)
<强>&GT; = RC.6 强>
在RC.6中,应该支持更新表单模型。创建新表单组并分配到myForm
[formGroup]="myForm"
也将受到支持(https://github.com/angular/angular/pull/11051#issuecomment-243483654)
<强>&GT; = RC.5 强>
form.reset();
在新表单模块中(&gt; = RC.5)NgForm
具有reset()
方法,并且还支持表单reset
事件。
https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179
<强>&LT = RC.3 强>
这将有效:
onSubmit(value:any):void {
//send some data to backend
for(var name in form.controls) {
(<Control>form.controls[name]).updateValue('');
/*(<FormControl>form.controls[name]).updateValue('');*/ this should work in RC4 if `Control` is not working, working same in my case
form.controls[name].setErrors(null);
}
}