3

I am building a very simple form like this:

  form = new FormGroup({
    choose: new FormControl('', Validators.required)
  })

I know that I can set my radio button to checked like this:

  form = new FormGroup({
    choose: new FormControl('1', Validators.required)
  })

My question is how to set radio button later. Maybe in ngOnInit()? Background is that I first do some API calls in init and after this I want to choose which radio button should be checked and which not. I don't want to do it in Template. Or is this only way?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
SaschaK
  • 170
  • 2
  • 15

1 Answers1

8

You can use the setValue() method after the API calls like this:

this.form.controls.choose.setValue('1');

Give this a read for a detailed answer regarding how to set the value to form control in Reactive Forms in Angular. Happy coding :)

Javapocalypse
  • 2,223
  • 17
  • 23