Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 986
Ibanez1408 Avatar asked Aug 30 '18 06:08

Ibanez1408


People also ask

How do I get mat-select value?

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.

What is compareWith in mat-select?

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>

How to get and set values in select option in Angular Material?

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.

How to create angular material select using MATLAB?

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.

How to bind a value with <MAT-option> in Angular Material 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.

What is matselectmodule in angular?

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.


3 Answers

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);
}
like image 149
Sivakumar Tadisetti Avatar answered Oct 13 '22 15:10

Sivakumar Tadisetti


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);
}
like image 29
rodzmkii Avatar answered Oct 13 '22 13:10

rodzmkii


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);
  }
like image 5
nipun-kavishka Avatar answered Oct 13 '22 13:10

nipun-kavishka