Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): fix radio button select timing #9646

Merged
merged 1 commit into from
Jun 27, 2016
Merged

fix(forms): fix radio button select timing #9646

merged 1 commit into from
Jun 27, 2016

Conversation

kara
Copy link
Contributor

@kara kara commented Jun 27, 2016

This PR ensures that if radio button controls are removed programmatically before their value accessors can be destroyed, they don't break the registry's selection logic.

Before

<input type="radio" [formControl]="showRadio" value="yes">
<input type="radio" [formControl]="showRadio" value="no">
<form [formGroup]="form">
   <div *ngIf="showRadio.value === 'yes'">
      <input type="radio" formControlName="food" value="chicken">
      <input type="radio" formControlName="food" value="fish">
   </div>
</form>
class MyComp {
   showRadio = new FormControl('yes');
   form = new FormGroup({
       'food': new FormControl('chicken')
   });

   constructor() {
      this.showRadio.valueChanges.subscribe((change) => {
         (change === 'yes') ? this.add() : this.remove());
      });
   }

   add() {
       this.form.addControl('food', new FormControl('chicken'));
   }

   remove() {
      // must process this next tick to avoid undefined control error from value accessor
      setTimeout(() => this.form.removeControl('food));
   }

}

After

   remove() {
      // don't need timeout
      this.form.removeControl('food');
   }

@kara kara added action: review The PR is still awaiting reviews from at least one requested reviewer and removed cla: yes labels Jun 27, 2016
@kara kara added the cla: yes label Jun 27, 2016
});
fixture.detectChanges();

var input = fixture.debugElement.query(By.css('[value="no"]'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got me!

@vsavkin vsavkin added pr_state: LGTM and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jun 27, 2016
@StephenFluin StephenFluin added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jun 27, 2016
@kara kara removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jun 27, 2016
@kara kara merged commit ed0ade6 into angular:master Jun 27, 2016
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants