I have a mat-select binded to an object Id, when I change the option I open a confirmation alert, I want the selected option to only change if I say OK if I cancel it the option I tried to select is kept selected, not on value but on display.
I tried setting the ngModel back to the same but that doesn't do anything, because my ngModel value isn't changed, only the selected option.
<mat-select title="Repartidor" placeholder="Repartidor" [ngModel]="order._ShippingProviderId" (selectionChange)="shippingProviderChange(order, $event)">
<mat-option *ngFor="let driverStatus of driverStatuses" [value]="driverStatus._ShippingProvider._Id">
{{driverStatus._ShippingProvider._Name}}
</mat-option>
</mat-select>
shippingProviderChange(order: IOrder, event: MatSelectChange){
if(confirm("¿Quieres asignar este repartidor?")) {
// change option
console.log(order._ShippingProviderId);
order._ShippingProviderId = event.value;
console.log(order._ShippingProviderId);
}
else{
// keep the same option selected
}
}
The mat-select keeps the option I didn't confirm selected instead of keeping the last one. Resetting the option to none is not an option since my options are binded to objects.
Update #1:
I tried this, and it does change the selected Value to the previous one BUT it doesn't set the selected option to it's original state.
else{
var same = order._ShippingProviderId;
order._ShippingProviderId = undefined;
order._ShippingProviderId = same;
}