Sur*_*iya 1 javascript mean-stack angular-material angular
我正在使用角材料。
问题出在mat-select. 它不绑定记录编辑时的值。
这是我的代码..
HTML
如您所见,我test.subject在模型中绑定了(一个对象)并subject.title在下拉列表中显示为文本。
<mat-form-field>
<mat-select [(ngModel)]="test.subject" placeholder="Subject" name="subject">
<mat-option>--</mat-option>
<mat-option *ngFor="let subject of subjects" [value]="subject">
{{subject.title}}
</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
成分
在组件中,我从数据库中获取了这个值。
this.test = {
"subject": {
"_id": "5b3883b4067d8d2744871eff",
"title": "Subject 1"
}
}
this.subjects = [
{
"_id": "5b3883b4067d8d2744871eff",
"title": "Subject 1"
},{
"_id": "5b3843b4067d8d2744435erx",
"title": "Subject 2"
}
]
Run Code Online (Sandbox Code Playgroud)
所以在这里我希望下拉菜单应该选择 value Subject 1。但事实并非如此。
嗨@Surjeet Bhadauriya
您可以尝试使用此解决方案。
我在Stackblitz上创建了一个演示
使用[compareWith]="compareObjects"使用对象mat-select
组件.html
<mat-form-field>
<mat-select [compareWith]="compareObjects" [(ngModel)]="test.subject" placeholder="Subject" name="subject">
<mat-option>--</mat-option>
<mat-option *ngFor="let subject of subjects" [value]="subject">
{{subject.title}}
</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
组件.ts
test: any;
subjects: Array<any>;
compareObjects(o1: any, o2: any): boolean {
return o1.name === o2.name && o1._id === o2._id;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4468 次 |
| 最近记录: |