I have an Angular Material app with radio buttons. I want to update the selected button based on data from a DB. It's all working except the checked button only updates when something happens on the page (e.g., a mouse click). Do I need to trigger change detection?
HTML
<mat-radio-group name="animals" (change)="saveAnimal($event)">
<mat-radio-button *ngFor="let option of animalOptions"
[checked]="option.checked" value="{{option.value}}">{{option.label}}
</mat-radio-button>
</mat-radio-group>
Component.ts
public animalOptions: any = [
{label: 'cat', value: '1', checked: true},
{label: 'dog', value: '2', checked: false}];
Then after calling API to get data in ngOnInit:
if (this._choice.animal === 'cat') {
this.animalOptions[0].checked = false;
this.animalOptions[1].checked = true;
}
this._choice.animalis populated exactly by updating the key aspects of that call in your question. Most likely, you are attempting to access that value outside of asubscribe()orthen()before it has resolved.