简体   繁体   中英

How can I clear my Angular reactive form?

I have a mat-radio-group inside my form and I would like to clear the radio button so that nothing is selected for each new question in my quiz app. How do I go about doing this? Here's how my form looks:

<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
  <ol class="form-group">
    <mat-radio-group formControlName="answer" name="answer" (change)="radioChange($event.value)"
  (click)="question.selectedOption = option">
      <div class="radio-options" *ngFor="let option of question.options">
        <mat-radio-button class="option" [value]="option.optionValue" disableRipple="true"
          [checked]="question.selectedOption == option"
          [ngClass]="{'is-correct': isCorrect(option.optionValue),
                  'is-incorrect': isIncorrect(option.optionValue)}">
          <li>{{ option.optionText }}</li>

You can reset your form:

reset() {
    this.myForm.reset();
  }

OR

like @miselking mentioned: Just reset specific control (in this case "radio button"). Something like this.myForm.get('radioButtonControl').reset()

Easiest and cleanest way to clear forms as well as their error states (dirty, pristine etc)

this.formName.reset();

for more info on forms readout here

https://angular.io/docs/ts/latest/guide/forms.html

OR

Just reset specific control (in this case "radio button")

this.myForm.get('radioButtonControl').reset()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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