【发布时间】:2020-02-28 14:28:42
【问题描述】:
很多问题都在讨论设置默认值以显示在“选择”控件中的方法,这里我公开了一个 Angular 8 模板驱动的表单案例,其中我无法获得在 mat-select 中显示的默认值单击按钮时,即使 console.log 显示正确的值:
<mat-select [formControl]="itemControl" required [(value)]="itemValue">
<mat-option>--</mat-option>
<mat-option *ngFor="let item of items" [value]="item">{{item}}</mat-option>
</mat-select>
我的组件代码部分如下:
export class MyComponent {
items: string[] = [''];
itemControl = new FormControl('', [Validators.required]);
itemValue: string = '';
myButtonClick(): void {
this.itemValue = this.getItems()[0]; <--- This returns the first element in a valid array
console.log(this.itemValue);
}
}
那我做错了什么?
【问题讨论】:
标签: angular typescript angular-material