简体   繁体   中英

How to get the value and the text in an Angular Material mat-select

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.

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

Working Stackblitz

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 properties value & source

  • value will hold selected option value and
  • To get the selected option text, you can simply call triggerValue on source like below

TS:

selectedValue(event: MatSelectChange) {
  this.selectedData = {
    value: event.value,
    text: event.source.triggerValue
  };
  console.log(this.selectedData);
}



Old Answer

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);
  }

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);
}
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM