Reputation: 5058
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.
Upvotes: 20
Views: 56731
Reputation: 5041
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);
}
Upvotes: 41
Reputation: 882
onSeletected(e: MatSelectChange): void {
setTimeout(() => {
let control = this.getControl('documentInfo');
control.setValue(e.source._elementRef.nativeElement.innerText);
}, 500);
}
I found solution for android 9, It worked for me. It would take time to change element after value change.
Upvotes: 1
Reputation: 1577
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);
}
Upvotes: 20
Reputation: 922
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);
}
Upvotes: 5
Reputation:
To get text and id of selected option:
In the .html file
<mat-select placeholder="Transaction Type" (change)="selected($event)" formControlName="TransactionTypeID">
<mat-option *ngFor="let t of transactionTypes" [value]="t.TransactionType" [id]="t.TransactionTypeId">
{{t.TransactionType}}
</mat-option>
</mat-select>
In the .ts file
import { MatAutocompleteSelectedEvent } from '@angular/material';
.....
selected(event:MatAutocompleteSelectedEvent) {
let id = event.option.id;;
let value = event.option.value;
console.log("id = "+ id + ", value: " + value);
}
Upvotes: 0