1
votes

I have mat-select on (selectionChange) event I'm receiving undefined as a value of $event.value. It returns correct value for first mat-option.mat-option with value 'All' it gives undefined

 <mat-form-field>
            <mat-select [(value)]="regionOption" (selectionChange)="regionSelectionChange($event)"
              placeholder="Region">
              <mat-option *ngFor="let region of regions" [value]="region.location_name">{{region.location_name}}</mat-option>
              <mat-option [value]="All" >All</mat-option>
            </mat-select>
 </mat-form-field>

//ts

regionSelectionChange(regionName)
{
     console.log(regionName);
}
2

2 Answers

1
votes

Change your value binding from [value] to value

<mat-option value="All">All</mat-option>

1
votes

I would go with this:

TS Code:

export class FormFieldOverviewExample {

  regions: any[]= [{'location_name':"Pune"},{'location_name':"Mumbai"}];

  constructor(){
    this.regions.push({'location_name':"All"}); -- Will add a value in the constructor
  }
  regionSelectionChange(regionName) {
    console.log(regionName.value);
  }
}

HTML Code:

<mat-form-field>
    <mat-select [(value)]="regionOption" (selectionChange)="regionSelectionChange($event)" placeholder="Region">
        <mat-option *ngFor="let region of regions" [value]="region.location_name">{{region.location_name}}
    </mat-option>
    </mat-select>
</mat-form-field>

Working-Demo