NGFOR未显示Firebase实时数据库中的数据 - Angular
ctustorService
export class CaptorsService {
private dbPath = "/auto/captor";
captorsRef: AngularFireList<any> = null;
constructor(private database: AngularFireDatabase) {
this.captorsRef = database.list(this.dbPath);
}
getAll(): AngularFireList<any> {
return this.captorsRef;
}
}
captorcomponent
export class CaptorsComponent implements OnInit {
captors: any[];
constructor(private captorsService: CaptorsService) {}
ngOnInit() {
this.retrieveCaptors();
}
public retrieveCaptors(): void {
this.captorsService
.getAll()
.snapshotChanges()
.pipe(map((changes) => changes.map((c) => ({ key: c.payload.key }))))
.subscribe((data) => {
this.captors = data;
});
}
}
<div *ngFor="let captor of captors">{{captor.key}}</div>
当数据从Firebase加载时,它显示在控制台上,但在DOM中没有显示任何内容。
CaptorService
export class CaptorsService {
private dbPath = "/auto/captor";
captorsRef: AngularFireList<any> = null;
constructor(private database: AngularFireDatabase) {
this.captorsRef = database.list(this.dbPath);
}
getAll(): AngularFireList<any> {
return this.captorsRef;
}
}
CaptorComponent
export class CaptorsComponent implements OnInit {
captors: any[];
constructor(private captorsService: CaptorsService) {}
ngOnInit() {
this.retrieveCaptors();
}
public retrieveCaptors(): void {
this.captorsService
.getAll()
.snapshotChanges()
.pipe(map((changes) => changes.map((c) => ({ key: c.payload.key }))))
.subscribe((data) => {
this.captors = data;
});
}
}
Template
<div *ngFor="let captor of captors">{{captor.key}}</div>
When the data is loaded from Firebase, It displayed on the console, but nothing showing in the DOM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在创建n个div,每个divs都为每个检索的项目。我不知道这是否是一种不好的做法,但是只要我知道,您不应该这样做。
您应该做的是,在 div 中,创建一个 list ,这样:
这就是我通常做这种事情的方式,如果您正确地填充了您的绑架者数组,这应该有效。
另外,如果要仔细检查绑架阵列是否填充了,请尝试创建一个在控制台中显示数组的按钮,例如:
html:
component: component:component:
You are creating n divs, each one for every item retrieved. I don't know if this is a bad practice, but as long as I know, you shouldn't do that.
What you should do is, inside of that div, create a list, like this:
This is how I usually do that kind of thing, and if you are populating properly your captor array, this should work.
Also, if you want to double check if your captor array is well populated, try to create a button that shows in the console that array, like this:
HTML:
COMPONENT: