【发布时间】:2021-04-13 18:07:56
【问题描述】:
我在 Angular Material 中使用mat-select,如下所示:
<mat-form-field class="full-width">
<mat-select (selectionChange)="dropdownSelectedItem($event)">
<mat-option *ngFor="let item of itemList" [value]="item">
{{item.name}}
</mat-option>
</mat-select>
</mat-form-field>
上面的下拉菜单很好,但下拉菜单没有默认值。
为了添加默认值,我添加了value="{{dropdownSelectedName}}",如下所示:
<mat-form-field class="full-width">
<mat-select (selectionChange)="dropdownSelectedItem($event)" value="{{dropdownSelectedName}}">
<mat-option *ngFor="let item of itemList" [value]="item">
{{item.name}}
</mat-option>
</mat-select>
</mat-form-field>
以上更改仍未填充默认下拉值。
我也试过[(value)]="dropdownSelectedName"
当我在组件中做console.log(this.dropdownSelectedName)时,我可以看到它有一个字符串值
mydefaultvalue
ngOnInit() {
this.subs.sink = this.myService.listAll()
.subscribe(r => {
this.AllList = r;
});
this.dropdownSelectedName = "mydefaultvalue";
}
【问题讨论】:
标签: html angular typescript angular-material dropdown