How to revert back Mat-Select's selected value for a specific option?

Paritosh

I am using mat-select, and my requirement is to revert back the selection for a specific value of mat-select's mat-option.

For example, Have a look at this stackblitz

Here mat-select has three options; when I select Canada, it should revert back to the value I've selected previously.

  onCountryChange($event) {
    const oldIndex = this.countries.indexOf(this.selectedCountry);
    if ($event.value.short === 'CA') {
      this.selectedCountry = this.countries[oldIndex];
    } else {
      this.selectedCountry = $event.value;
    }
  }

I can see that the value is getting updated from the json besides the mat-select. However, the mat-select is not updating the selected option.

So far I tried setting value using FormControl, setTimeout, by attaching just short property of the object instead of attaching entire object with mat-option (this stackblitz for reference). But couldn't make it work.

pixelbyaj

I think you should use mat-select-trigger. This is a due to the mat-select using OnPush change detection. You may be interested in using mat-select-trigger. Try that and let me know if it works for you.

<mat-select name="countryVaraible" [value]="selectedCountry" placeholder="Country" (selectionChange)="onCountryChange($event)">
    <mat-select-trigger>{{selectedCountry.full}}</mat-select-trigger>
    <mat-option *ngFor="let country of countries" [value]="country">
      {{country.full}}
    </mat-option>
</mat-select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get selected option value from specific select

From Dev

How to get the option value of selected in Select dropdown

From Java

Set select option 'selected', by value

From Dev

How to change Selected Option to specific value on close of Overlay window?

From Dev

How do I to set selected value in a select option using jquery?

From Dev

How do I to set selected value in a select option using jquery?

From Dev

How to get the selected option value from select inside div

From Dev

Don't allow to select other option if selected specific value with select2

From Dev

Is it possible to target a <select> with a specific option selected?

From Dev

Alert or message on specific option selected in select menu

From Dev

How to set the selected option of <select>

From Dev

How to set the selected option of <select>

From Dev

Set selected option by option's attribute value

From Dev

How to Fire an Event with Jquery when a select value is unchanged for a specific option or value is changed to a specific option

From Dev

How to retrieve selected option's value in Laravel 5 dropdown list?

From Dev

Change class of html element when specific option/value is selected in select field

From Dev

How to get specific value from option select - json object

From Dev

How to revert back to ALSA?

From Dev

Value of <select><option> coming back as string

From Java

selected in select option give wrong attribute value

From Dev

Keep the value of an already selected radio/select option

From Dev

show option value selected in form select

From Dev

Access value of selected option in <select> element with PHP

From Dev

Select option selected an not selected

From Dev

Show a different text for the selected option for `<select>`s

From Dev

How to disable <select> option based on other <select> option selected?

From Dev

How to make the Selected Option the width of the current Selected Option's text?

From Dev

How to get the value of a select's option by its text

From Dev

How can I hide an option of a select by it's text value?

Related Related

  1. 1

    Get selected option value from specific select

  2. 2

    How to get the option value of selected in Select dropdown

  3. 3

    Set select option 'selected', by value

  4. 4

    How to change Selected Option to specific value on close of Overlay window?

  5. 5

    How do I to set selected value in a select option using jquery?

  6. 6

    How do I to set selected value in a select option using jquery?

  7. 7

    How to get the selected option value from select inside div

  8. 8

    Don't allow to select other option if selected specific value with select2

  9. 9

    Is it possible to target a <select> with a specific option selected?

  10. 10

    Alert or message on specific option selected in select menu

  11. 11

    How to set the selected option of <select>

  12. 12

    How to set the selected option of <select>

  13. 13

    Set selected option by option's attribute value

  14. 14

    How to Fire an Event with Jquery when a select value is unchanged for a specific option or value is changed to a specific option

  15. 15

    How to retrieve selected option's value in Laravel 5 dropdown list?

  16. 16

    Change class of html element when specific option/value is selected in select field

  17. 17

    How to get specific value from option select - json object

  18. 18

    How to revert back to ALSA?

  19. 19

    Value of <select><option> coming back as string

  20. 20

    selected in select option give wrong attribute value

  21. 21

    Keep the value of an already selected radio/select option

  22. 22

    show option value selected in form select

  23. 23

    Access value of selected option in <select> element with PHP

  24. 24

    Select option selected an not selected

  25. 25

    Show a different text for the selected option for `<select>`s

  26. 26

    How to disable <select> option based on other <select> option selected?

  27. 27

    How to make the Selected Option the width of the current Selected Option's text?

  28. 28

    How to get the value of a select's option by its text

  29. 29

    How can I hide an option of a select by it's text value?

HotTag

Archive