How to get the id of selected option value in mat-select angular 5. Get only value of selected option in onchangeevent. but how can get id of selected option value.
client.component.html <mat-form-field> <mat-select placeholder="Client*" #clientValue (change)="changeClient($event)"> <mat-option *ngFor="let client of clientDetails" [value]="client.clientName"> {{client.clientName | json}} </mat-option> </mat-select> </mat-form-field> client.component.ts file export class Client{ changeClient(event){ console.log(event); } }
Create Select using <mat-select> Angular Material Select is created using <mat-select> which is a form control for selecting a value from a set of options. To add elements to select option, we need to use <mat-option> element. To bind value with <mat-option> , we need to use value property of it.
MatSelectTrigger. Allows the user to customize the trigger that is displayed when the select has a value.
What is a Selector in Angular? A selector is one of the properties of the object that we use along with the component configuration. A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented in the HTML DOM.
The question is specific to Angular 5 but for others coming here with a newer version of Angular, the (change)
event won't work for mat-select.
In Angular 6 the (change)
event has been changed to (selectionChange)
.
So it would be:
<mat-form-field> <mat-select placeholder="Client*" #clientValue (selectionChange)="changeClient($event.value)"> <mat-option *ngFor="let client of clientDetails" [value]="client.id"> {{client.clientName}} </mat-option> </mat-select> </mat-form-field>
And in the component:
changeClient(value) { console.log(value); }
From this answer and the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With