Mat select - Get old value of selectionChange

Discussão em 'Angular' iniciado por Ling Vu, Dezembro 30, 2024.

  1. Ling Vu

    Ling Vu Guest

    I have a project in which I have a form containing a mat-select selector. Whenever the user changes the input, I show the user a dialog to confirm this operation. Now:

    The selectionChange() notifies when the value was changed and passes the new value as $event.

    Is there a way to get the old value to return back in case when the user canceled the dialog?

    I don't want to do the dirty way like, saving the current value in a seperate member variable.

    Template:

    <mat-select [(ngModel)]="selectedTextCountingType"
    (selectionChange)="select($event)">
    <mat-option *ngFor="let option of countingTypeOptions"
    [value]="option.value">
    {{option.text | translate}}
    </mat-option>
    </mat-select>


    TS:

    select(option: MatSelectChange): void {
    this.openConfirmDeletionDialog().pipe(
    filter((respose) => {
    // HERE IS WHERE I NEED IT.
    if(Boolean(reponse) === false) {
    this.selectedTextCountingType = oldValue;
    }
    return Boolean(reponse);
    })
    )
    }

    Continue reading...

Compartilhe esta Página