Angular 6 Material mat-select change method removed
Asked Answered
V

7

217

In Angular Material Design 6, the (change) method was removed. How should I replace the change method to execute code in the component when the user changes selection?

Vamoose answered 7/5, 2018 at 21:31 Comment(0)
F
539

The changed it from change to selectionChange.

<mat-select (change)="doSomething($event)">

is now

<mat-select (selectionChange)="doSomething($event)">

https://material.angular.io/components/select/api

Fourthclass answered 7/5, 2018 at 23:55 Comment(7)
I hate this so much. Yesterday I thought it was a cool day to upgrade to Angular 6. Once again, they changed the syntax.Ulita
They need a (changeEventChange) event to detect when the change event changes.Unhandled
(selectionChange) is now updated to (onSelectionChange).Connective
@Connective - Where did you see this at? I still see selectionChange material.angular.io/components/select/apiFourthclass
All the comments above only further highlight why it makes sense to stick to a reactive forms appoach, as per my answer below, where possible. On a side note, I don’t think the comments are very fair because the Angular Material guys do an amazing job and we get it for free.Thalassography
@Fourthclass I used both methods, selectionChange was not working, then I read in another stackOverflow answer, where it was mentioned.Connective
<mat-select [(ngModel)]="Zone" (selectionChange)="BasedonZone(Zone)">Rechabite
T
32

If you're using Reactive forms you can listen for changes to the select control like so..

this.form.get('mySelectControl').valueChanges.subscribe(value => { ... do stuff ... })
Thalassography answered 7/5, 2018 at 22:40 Comment(6)
It's worth noting that using the above reactive forms approach you are less tied to the UI and less likely to have problems as that UI framework evolvesThalassography
Just to note here that if you need to .updateValueAndValidity the control don't forget to pass { emitEvent: false } in order to avoid RangeError: Maximum call stack size exceeded. On the other hand thanks for hint (+1), it lead me to what I needed.Sterling
Should I unsubscribe in the ngOnDestroy if I follow the approach?Illusage
You always need to cleanup your subscriptions to avoid memory leaks and unexpected behaviour.Thalassography
you can also go with this.form.controls.yourControlName.valueChanges.subscribe(values => { console.log('new value', value); }Font
@Joseph using your approach how can I get an index of the selected item?Freefloating
A
18

For:

1) mat-select (selectionChange)="myFunction()" works in angular as:

sample.component.html

 <mat-select placeholder="Select your option" [(ngModel)]="option" name="action" 
      (selectionChange)="onChange()">
     <mat-option *ngFor="let option of actions" [value]="option">
       {{option}}
     </mat-option>
 </mat-select>

sample.component.ts

actions=['A','B','C'];
onChange() {
  //Do something
}

2) Simple html select (change)="myFunction()" works in angular as:

sample.component.html

<select (change)="onChange()" [(ngModel)]="regObj.status">
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>

sample.component.ts

onChange() {
  //Do something
}
Alvardo answered 26/8, 2019 at 13:58 Comment(0)
B
10

For me (selectionChange) and the suggested (onSelectionChange) didn't work and I'm not using ReactiveForms. What I ended up doing was using the (valueChange) event like:

<mat-select (valueChange)="someFunction()">

And this worked for me

Bilbe answered 22/4, 2019 at 13:36 Comment(1)
I'm using a template form, and worked to me using the following: <mat-select placeholder="Select an option" [(ngModel)]="project.managerId" name="managerId" required (selectionChange)="fillComanager(project.managerId)"> <mat-option *ngFor="let manager of managers" [value]="manager.id"> {{ manager.name }} </mat-option> </mat-select>Belostok
C
5

I have this issue today with mat-option-group. The thing which solved me the problem is using in other provided event of mat-select : valueChange

I put here a little code for understanding :

<mat-form-field >
  <mat-label>Filter By</mat-label>
  <mat-select  panelClass="" #choosedValue (valueChange)="doSomething1(choosedValue.value)"> <!-- (valueChange)="doSomething1(choosedValue.value)" instead of (change) or other event-->

    <mat-option >-- None --</mat-option>
      <mat-optgroup  *ngFor="let group of filterData" [label]="group.viewValue"
                    style = "background-color: #0c5460">
        <mat-option *ngFor="let option of group.options" [value]="option.value">
          {{option.viewValue}}
        </mat-option>
      </mat-optgroup>
  </mat-select>
</mat-form-field>

Mat Version:

"@angular/material": "^6.4.7",

Campanile answered 23/1, 2020 at 16:3 Comment(1)
Thx for the answer, i was looking for this to get the value and viewValue from the html-binding. With selectionChange it does only send the value but not the viewValue via $event.Yester
B
2

<mat-select (change)="doSomething($event)">

is now <mat-select (selectionChange)="doSomething($event)">

Bagley answered 2/9, 2022 at 10:49 Comment(1)
Works for me as of Angular 16.1.0Cembalo
E
1

For me ...(click)="myFunction()"... worked; nothing from above.

Exeter answered 15/7, 2021 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.