Reputation: 1274
I have following html file
<mat-menu #appMenu="matMenu" class="mat-menu-panel-max-width-none">
<mat-radio-group #radioGroup>
<div *ngFor="let item of items, let i=index">
<mat-radio-button color="primary"
[value]="item.value"
[checked]="item.selected"
class="mat-menu-item"
(change)="handleSelection(i)">
{{item.name}}
</mat-radio-button>
</div>
</mat-radio-group>
</mat-menu>
And ts file.
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'item-selection',
templateUrl: './item-selection.component.html',
})
export class ItemsComponent {
@Input() items: any;
@Output() electionHandler = new EventEmitter();
constructor() { }
handleSelection(id) {
electionHandler.emit({id:id});
}
}
Here are steps my code is doing:
What is wrong?
Upvotes: 1
Views: 1774
Reputation: 189
Instead of trying to manually manage the 'checked' state of the radio button. I would personally try to leverage the native HTML functionality and give the radios the same 'name' value of the same value or if your using Reactive Forms, the formControlName property will also accomplish the same thing. And they 'check' and 'uncheck' themselves as the user selects from the form.
Upvotes: 1