Arrays 使用ngFor对从Firebase获得的对象进行迭代
我有一个存储在firebase数据库中的对象列表: 我使用http get请求获取此列表。得到它后,我想使用Arrays 使用ngFor对从Firebase获得的对象进行迭代,arrays,angular,typescript,firebase,object,Arrays,Angular,Typescript,Firebase,Object,我有一个存储在firebase数据库中的对象列表: 我使用http get请求获取此列表。得到它后,我想使用ngFor=“let subject of subjects”在html模板中迭代li,这会给出错误: 错误:找不到不同的支持对象“[object]” 类型为“对象”。NgFor仅支持绑定到Iterables,例如 数组 当我在网上搜索时,我知道当我获取嵌套对象时,ngFor只能用于数组。谁能建议我迭代这些对象应该做什么 到目前为止,我已经尝试了Array.of,它将整个列表转换为som
ngFor=“let subject of subjects”
在html模板中迭代li,这会给出错误:
错误:找不到不同的支持对象“[object]”
类型为“对象”。NgFor仅支持绑定到Iterables,例如
数组
当我在网上搜索时,我知道当我获取嵌套对象时,ngFor只能用于数组。谁能建议我迭代这些对象应该做什么
到目前为止,我已经尝试了Array.of
,它将整个列表转换为someArray[0]
。我还尝试手动将对象的唯一id更改为数组索引[0,1,2]
,这很有效,但当我使用http post
firebase添加新主题时,会自动将唯一id分配给新主题,使其可统一操作
简单地说,告诉我如何将嵌套对象转换为arrayList,或者如何更改firebase在angular中指定唯一id的默认行为(我发现了一些类似firebase push函数的东西,我无法理解)
更新:
(来自审查服务的代码)
(来自组件的代码)
(来自HTML模板的代码)
美国号
主题名称
促进者
{{i+1}}
{{subject.name}
{{主题.主持人}
尝试以下代码片段
ngFor="let subject of subjects|async
更新
在您的ExamierService
中,您应该导入FirebaseListObservable
,以便定义返回类型FirebaseListObservable
在您的组件中,应该如下所示
export class ExaminerComponent implements OnInit {
movies: any[];
constructor(private examinerDb: ExaminerService) { }
ngOnInit() {
this.examinerDb.get().subscribe((snaps) => {
this.selectedBatchData = snaps;
});
}
}
可以使用将属性转换为数组。这可以通过几种方式实现
方法1-通过对可观察数据应用
惊人的胡子!谢谢@PeterHaddad,你能帮我解决这个问题吗(不起作用。错误:InvalidPipeArgument:“[object object]”对于管道“AsyncPipe”,您的方法应该返回
get():FirebaseListObservable{返回this.db.list(“/subjects”);}
它是如何工作的?你甚至没有从响应中提取json。让我们来看看。伙计,你太棒了。非常感谢你。我从一整天以来就在寻找这个解决方案。不客气。我添加了一个方法,用映射
操作符来完成。你不需要,因为任何数组
也不好。@AluanHaddad-没有它可能工作但是Visual Studio代码和stackblitz中的语法检查器在红色的Object.values
(“ObjectConstructor”类型上不存在属性“values”),我不希望看到这种情况。@RobertWilliams-在所有这些之后,别忘了使用map
操作符查看该方法。
<div class="batchDetails">
<table class="table table-striped">
<thead>
<tr>
<th class="text-center">S.No</th>
<th>Subject Name</th>
<th>Facilitator</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let subject of selectedBatchData.subjects; let i = index">
<td class="text-center"> {{i + 1}} </td>
<td> {{subject.name}} </td>
<td> {{subject.facilitator}} </td>
</tr>
</tbody>
</table>
</div>
ngFor="let subject of subjects|async
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
export class ExaminerService{
constructor(private db: AngularFireDatabase) {}
getBatchSubjects(batch){
return this.db.list('/subjects');
}
}
export class ExaminerComponent implements OnInit {
movies: any[];
constructor(private examinerDb: ExaminerService) { }
ngOnInit() {
this.examinerDb.get().subscribe((snaps) => {
this.selectedBatchData = snaps;
});
}
}
import { Observable } from "rxjs/Rx";
public subjects: Array<any>;
this.examinerService.getBatchSubjects(this.selectedBatch)
.map(x => (Object as any).values(x.json().subjects))
.subscribe(
(values) => { this.subjects = values; },
(error) => { console.log(error); }
);
get subjects(): Array<any> {
return (Object as any).values(this.selectedBatchData.subjects);
}
public subjects: Array<any>;
...
this.selectedBatchData = response.json();
this.subjects = (Object as any).values(this.selectedBatchData.subjects);
<tr *ngFor="let subject of subjects; let i = index">
<td class="text-center"> {{i + 1}} </td>
<td> {{subject.name}} </td>
<td> {{subject.facilitator}} </td>
</tr>