首页 > 解决方案 > mat-select 下拉补丁值不起作用?

问题描述

HTML :(这里我有下拉菜单来获取选定的值。我使用值绑定来获取值。我也尝试过 ngModel 但它不起作用。我的代码有什么问题?你能帮帮我吗?)

        <mat-form-field>
            <mat-select  placeholder="Team NO" formControlName="team" [(value)]="teamNO" required>
                <mat-option *ngFor="let list of teamList" [value]="list" >{{list}}</mat-option>
            </mat-select>
        </mat-form-field>

组件.ts

      teamList = ['1','2','3','4','5','6','7','8','9','10'];
      teamNO : any;
     ngOnInit(){
        this.operatorService.currentEditSchedule.subscribe((result: any) =>{
           if(!!result){
            console.log(result)
            this.teamNO = result.teamNo 
            }
         })
       }

标签: javascriptangular

解决方案


Try to use ngModel instead of value in mat-select:

     <mat-form-field>
        <mat-select  placeholder="Team NO" formControlName="team" [(ngModel)]="teamNO" required>
            <mat-option *ngFor="let list of teamList" [value]="list" >{{list}}</mat-option>
        </mat-select>
    </mat-form-field>

stackblitz

In case you get a number from your backend, it can help to convert it to a string:

this.teamNO = result.teamNo + '';

推荐阅读