I need to get data of a mat-select specifically the text and its value. This is how I implemented the mat-select so far..
<mat-select
placeholder="Transaction Type"
(selectionChange)="selected($event)"
formControlName="TransactionTypeID"
>
<mat-option *ngFor="let t of transactionTypes" [value]="t.TransactionTypeID">
{{ t.TransactionType }}
</mat-option>
</mat-select>
This is how I get the value in the .ts file: this.formDetail.get('TransactionTypeID').value,
This is my attempt to get the text or 't.TransactionType':
selected(event: MatSelectChange) {
console.log(event);
}
Can you please show me how to do this? Thank you.
To add elements to Select option, we need to use <mat-option> element and to bind value with <mat-option> , use value property of it. To set and get a value for <mat-select> , use value , ngModel , formControl and formControlName property.
The compareWith just literally compares objects by some given values and the checkboxes get selected, if it returns true . In my code, I decided to go with the ng-Model binding but it works with formControl as well: <mat-form-field>
We can use NgModel to get and set values in Select option for Angular Material Select as well as native Select. Suppose we have component property. Use ngModel with <mat-select> to set and get value as following. Use ngModel with native <select> to set and get value as following.
To create Angular Material select either by <mat-select> or native <select>, we need to keep them inside <mat-form-field> element. For multiple selection, we need to use multiple attribute with <mat-select> and native <select>. Now let us discuss to work with Angular Material Select in detail step-by-step.
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.
MatSelectModule is the API reference for Angular Material select. 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.
Update : 2020 (Updated answer as per the new version of angular material)
The below old answer worked for the OP at the time question was asked. But I observed comments on the old answer and output event, change
of mat-select
has been deprecated in the new version of angular material. So, the correct answer is
HTML:
<mat-form-field>
<mat-select (selectionChange)="selectedValue($event)">
<mat-option [value]="'GB'">Great Britain</mat-option>
<mat-option [value]="'US'">United States</mat-option>
<mat-option [value]="'CA'">Canada</mat-option>
</mat-select>
</mat-form-field>
selectionChange
will give us an object contains 2 propertiesvalue
&source
value
will hold selected option value andtriggerValue
on source
like belowTS:
selectedValue(event: MatSelectChange) {
this.selectedData = {
value: event.value,
text: event.source.triggerValue
};
console.log(this.selectedData);
}
With normal change event, you can get the value like below
In the .html file
<mat-select placeholder="Transaction Type" (change)="selected($event)" formControlName="TransactionTypeID">
<mat-option *ngFor="let t of transactionTypes" [value]="t.TransactionTypeID">
{{t.TransactionType}}
</mat-option>
</mat-select>
In the .ts file
selected(event) {
let target = event.source.selected._element.nativeElement;
let selectedData = {
value: event.value,
text: target.innerText.trim()
};
console.log(selectedData);
}
To complete preceding answer one can use viewValue of MatOption. Also Angular 7 compiler wants to know if event.source.selected should be an array or a single object.
selected(event: MatSelectChange) {
const selectedData = {
text: (event.source.selected as MatOption).viewValue,
value: event.source.value
};
console.log(selectedData);
}
It can easily do with this code.
HTML
<mat-select value="option1" (selectionChange)=changeRatio($event)>
<mat-option value="option0">Free Selection</mat-option>
<mat-option value="option1">1 : 1.5 (Recommended)</mat-option>
</mat-select>
Angular TS
changeRatio(event: MatSelectChange) {
console.log(event.value);
}
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