【发布时间】:2019-06-13 01:21:02
【问题描述】:
我想显示name,但将_id 的值保存为mat-select。
并将其值保存在selectedIngridient 中,用于选择选项。
我明白了:
“找不到类型为‘object’的不同支持对象‘[object Object]’。NgFor 仅支持绑定到 Iterables,例如数组。”
但我正在查看不同的帖子(例如:Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays),无法找出错误。
我做错了什么?
斯塔克利茨:
https://stackblitz.com/edit/angular-l6yvgd?file=src/app/app.module.ts
初始化:
import { Observable } from 'rxjs/internal/Observable';
allIngridients: Observable<Ingridient[]>;
selectedIngridient: Ingridient;
数据层:
export class Ingridient {
_id: string;
name: string;// todo: Übersetzungen
barCode?: Barcode;
constructor() {
this._id = UUID.UUID();
}
}
标记:
<mat-form-field>
<mat-select placeholder="Select an ingridient" [(value)]="selectedIngridient">
<mat-option *ngFor="let ingridient of allIngridients" [value]="ingridient._id">
{{ingridient.name}}
</mat-option>
</mat-select>
</mat-form-field>
填写allIngridients(来电constructor):
inlineFunction_IngridientsNumberAndUnits_Ingridient(recipeId: string, anzahl: number): Ingridient[] {
let result: Ingridient[] = [];
for (let i_result = 0; i_result < anzahl; i_result++) {
// todo: später mit recipe
const tag: Ingridient = new Ingridient(recipeId);
// Code
tag.name = "Unit_" + (i_result + 1);
tag.name = "BarCodes_" + (i_result + 1);
// todo: changed
this.allIngridients.subscribe(current => {
current.push(tag);
})
result.push(tag);
}
return result;
}
【问题讨论】:
-
我正在为您撰写答案,但 Jota 将其标记为 dup。
标签: angular typescript