We can use [(ngModel)] as follows and have a radioSelected value selector
Textbook example
Demo link
app.component.html
<div class="text-center mt-5"> <h4>Selected value is {{radioSel.name}}</h4> <div> <ul class="list-group"> <li class="list-group-item" *ngFor="let item of itemsList"> <input type="radio" [(ngModel)]="radioSelected" name="list_name" value="{{item.value}}" (change)="onItemChange(item)"/> {{item.name}} </li> </ul> </div> <h5>{{radioSelectedString}}</h5> </div>
app.component.ts
import {Item} from '../app/item'; import {ITEMS} from '../app/mock-data'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app'; radioSel:any; radioSelected:string; radioSelectedString:string; itemsList: Item[] = ITEMS; constructor() { this.itemsList = ITEMS;
Listing Data Examples
export const ITEMS: Item[] = [ { name:'Item 1', value:'item_1' }, { name:'Item 2', value:'item_2' }, { name:'Item 3', value:'item_3' }, { name:'Item 4', value:'item_4' }, { name:'Item 5', value:'item_5' } ];
Code spy
source share