我有一个 mat-select,其中所有选项都是在数组中定义的对象。我试图将该值设置为默认选项之一,但在页面呈现时,它仍然被选中。
我的 TypeScript 文件包含:
public options2 = [
{"id": 1, "name": "a"},
{"id": 2, "name": "b"}
]
public selected2 = this.options2[1].id;
我的HTML文件包含:
<div>
<mat-select
[(value)]="selected2">
<mat-option
*ngFor="let option of options2"
value="{{ option.id }}">
{{ option.name }}
</mat-option>
</mat-select>
</div>
我已经尝试将mat-option
中的selected2
和value
设置为对象及其ID,并在mat-select
中同时使用了[(value)]
和[(ngModel)]
,但都没有起作用。
我正在使用Material版本2.0.0-beta.10。
compareWith
,更加优雅。 - Badis MerabetcompareWith
,请参考这里的badis答案 https://dev59.com/JFYN5IYBdhLWcg3whIXz - bresleveloper