1

I am working in Angular where I am facing problem in

getting index of value from Array iterated in dropdown Box

I am sharing my code

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  fruit =  [{"id" : 123 , "name" : "Apple" } , {"id" : 124 , "name" : "Orange" } ]

  constructor(){}

  getRsId(value , Index){}
}

html

<select (ngModelChange)="getRsId($event , i)">
  <option [value]='data.emp_code' *ngFor='let data of fruit ; let i = index' > {{data.name}} </option>
</select>

https://stackblitz.com/edit/angular-p6deib?file=src%2Fapp%2Fapp.component.html

ANURAG RANJAN
  • 115
  • 2
  • 16

1 Answers1

1

Change this :

<select (change)="getRsId($event)">
  <option [value]='data.emp_code' *ngFor='let data of fruit ; let i = index' > {{data.name}} </option>
</select>
 getRsId(value){
   console.log(value);
   console.log(value.target['selectedIndex']);
  }

Here is demo : https://stackblitz.com/edit/angular-luhanl?file=src/app/app.component.ts

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53