保留Angular中垫选的值

时间:2019-08-29 08:01:03

标签: angular

我有一个席子选择字段。 mat-select字段的值是从数组中填充的。 当我在下拉菜单中选择另一个项目时,将显示一个带有“确定”和“取消”按钮的对话框。当我选择确定时,我选择的项目  将被设置为选定项目。否则,前一项 需要设置为选中项。 目前,我无法上一个项目。如果我做出选择,它将被设置为选中项。

html

<mat-select [(ngModel)]="selectedCar" fxFlex="12.5rem" 
  (selectionChange)="onCarChange(selectedCar)" id="slctselectedCar">
 <mat-option *ngFor="let carName of carNames" [value]="carName">
    {{carName}}  
 </mat-option>
</mat-select>

.ts文件

carNames:string[]=["Toyota","Honda"];  

public onCarChange(selectedCar): void{
  console.log(selectedCar);
}

1 个答案:

答案 0 :(得分:1)

如果我做对了,您需要这样的东西:

  public onCarChange(selectedCar): void {
       this.possibleSelectedItem = selectedCar;

       // dialog box code here

       // if OK then this.selectedItem = this.possibleSelectedItem
       // if Cancel, do nothing and selected item will remain the previous one
  }

您可以在没有this.possibleSelectedItem的情况下做到这一点,但也许这样更容易理解。

Here是一个StackBlitz示例,没有对话框代码