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?

1

1 Answer 1

9

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.