1

Stackblitz I am trying to find the id associated with the name I select using the radio button. Any help?

<form #filterForm="ngForm" (ngSubmit)="addFilter(filterForm)" autocomplete="off">
    <div class="row">
        <div class="col-12" style="border: 1px solid red">
            <div *ngFor="let x of countryArray">
                <label for="{{x.name}}"> {{x.name}} </label>
                <input type="radio" id="{{x.name}}" value="{{x.name}}" name="name" [(ngModel)]='selected'>
                            </div>
            </div>
            <hr />

            <button type="submit">SAVE</button>

        </div>
</form>
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
MenimE
  • 274
  • 6
  • 18
  • `console.log(this.countryArray.filter(e => e.id === 1));` – Nicholas K Dec 16 '19 at 17:19
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Nicholas K Dec 16 '19 at 17:20

1 Answers1

0

try to declare a property and set the value on model change event

<div *ngFor="let x of countryArray">
    <label for="{{x.name}}"> {{x.name}} </label>
    <input type="radio" id="{{x.name}}" value="{{x.name}}" name="name" 
       ngModel (ngModelChange)="selectedValue = x">
  </div>

demo

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91