我有对象 Categories(id, name),我在 Angular 上是这样的:
this.categories = new Array<Category>();
this.http
.get(ApiConfig.API_URL + 'getCategories')
.toPromise()
.then(response => this.categories = response.json())
.catch(_ => console.log("error getting categories"));
对象 Categories.ts:
export class Category
{
constructor(
public id: string,
public name: string)
{ }
}
我正试图将它们作为选项放在自动完成字段中,如下所示:
<div>
<md-form-field>
<input type="text" mdInput [mdAutocomplete]="auto">
</md-form-field>
<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let category of categories">
{{ category.name }}
</md-option>
</md-autocomplete>
</div>
但是我得到以下错误
Error trying to diff '[object Object]'. Only arrays and iterables are allowed
我试过
<md-option *ngFor="let category of categories" [value]="category.id" [label]="category.name">
但它仍然抛出同样的错误,我做错了什么?
最佳答案
看你评论的json,需要遍历categories.result
。将您的 *ngFor
更改为以下内容:
<md-option *ngFor="let category of categories.result">
{{ category.name }}
</md-option>
关于html - ngFor 在传递数组时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043337/