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>
component.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
}
})
}
答案 0 :(得分:1)
尝试使用ngModel代替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>
如果您从后端获取了一个数字,则可以帮助将其转换为字符串:
this.teamNO = result.teamNo + '';