Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular mat-select get the selected option data object

Under my Angular 7 app , i'm using the mat-select component :

<mat-form-field  class="col-md-3" color="warn">
     <mat-select placeholder="Selectionner le code Edo"
                  id="codeEdo"
                  [(value)]="selectedCodeEdoValue"
                  ngDefaultControl
                  formControlName="codeEdo">
     <mat-option (onSelectionChange)="onEdoSelectionChanged(edo )"
                 *ngFor="let edo of shopsList"
                  [value]="edo.edoId">
                  {{edo.edoId}}
     </mat-option>
     </mat-select>
</mat-form-field>

My options data are the following:

let shopsList= [
  {
    'edoId': '4010',
    'storeName': 'ABBEVILLE'
  }
  ,
  {
    'edoId': '3650',
    'storeName': 'AGEN'
  }
  ,
  {
    'edoId': '3298',
    'storeName': 'AIX ALLEES PROVENCALES'
  }
];

My purpose is to get the selected object data when an option is selected , and not just the value

for example when i select the first option , i wanna get this :

result = {
    'edoId': '4010',
    'storeName': 'ABBEVILLE'
}

And not only the value '4010'

As you can see i ve tried this : (onSelectionChange)="onEdoSelectionChanged(edo )

But this fires me always two events , for the old selected option and the new selected option ,

How may i only get the new selection data ??

like image 755
firasKoubaa Avatar asked Mar 04 '23 08:03

firasKoubaa


1 Answers

I think your error is with this bit of code:

[value]="edo.edoId"

Try changing it to

[value]="edo"

This will return the full object, instead of just that specific field

like image 193
bsheps Avatar answered Mar 11 '23 01:03

bsheps