简体   繁体   English

Angular2表单重置,在表单重置后取消选中广播或提交

[英]Angular2 form reset, uncheck radios after form reset or submit

How to reset form in ng2, I allready use method form.reset(), but after reset I have still checked checkboxes on form. 如何在ng2中重置表单,我已经使用了form.reset()方法,但是重置后我仍然选中了表单上的复选框。 Question is, how can I uncheck checkboxes in form? 问题是,如何取消选中表单中的复选框?

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

Regards! 问候! Bosper 博斯珀

You may have to reset each form control. 您可能必须重置每个窗体控件。

form.reset() can take an object comprised of all formcontrols. form.reset()可以接受一个由所有formcontrol组成的对象。 Since you haven't posted how you are using form.reset, try the below. 由于尚未发布如何使用form.reset,请尝试以下操作。

For example, try something like: 例如,尝试以下操作:

this.taskCreate.reset({
goal: '',
prodCodeList: '',
note: '',
priority: '',
attachmentList: ''
})

This should reset each formcontrol you have, which means they will be untouched and pristine. 这将重置您拥有的每个FormControl,这意味着它们将保持原状。

Docs here . 文档在这里

>=RC.6 > = RC.6

In RC.6 it should be supported to update the form model. 在RC.6中,应支持更新表单模型。 Creating a new form group and assigning to myForm 创建一个新的表单组并分配给myForm

[formGroup]="myForm"

will also be supported ( https://github.com/angular/angular/pull/11051#issuecomment-243483654 ) 也将被支持( https://github.com/angular/angular/pull/11051#issuecomment-243483654

>=RC.5 > = RC.5

form.reset();

In the new forms module (>= RC.5) NgForm has a reset() method and also supports a forms reset event. 在新的表单模块(> = RC.5)中, NgForm具有reset()方法,并且还支持表单reset事件。 https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179 https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179

<=RC.3 <= RC.3

This will work: 这将起作用:

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

ref: How to clear form after submit in Angular 2 参考: 在Angular 2中提交后如何清除表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM