【发布时间】:2021-02-19 10:34:55
【问题描述】:
我正在尝试显示来自 firebase 存储的图像,如下所示:
在我的组件中:
findImg(img) {
this.storage.ref('/img/' + img).getDownloadURL().subscribe(
result => {
console.log(result);
}
)
在我的模板中:
<ion-header>
<ion-toolbar>
<ion-title>La gamme : {{collectionName}}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card *ngFor="let pro of product | async">
<ion-card-header>
<ion-card-title>{{pro.name}}</ion-card-title>
</ion-card-header>
<ion-card-content>
<p>{{pro.img}}</p>
<div *ngIf="findImg(pro.img)"></div>
</ion-card-content>
</ion-card>
</ion-content>
pro.img 是我的产品数据库中字段“img”的名称,也是他存储中图片的名称。
除了带有 ngIf 的 div 之外,一切都有效。在控制台中,结果很好,但显示无限,Chrome 崩溃。我不明白为什么循环永远不会结束。
【问题讨论】:
-
我的数据库中只有 2 个产品
-
这可能是因为每次运行更改检测时都会调用此方法
findImg(pro.img),这可能就是您在控制台中收到这些消息的原因。您可以尝试用变量替换它吗? -
我明白你说的,例如 findImg($myVariable) ?但是我怎样才能得到这个变量?我很抱歉,但我是初级 :) 这应该修改我的代码,但我现在不知道如何隔离这些数据。
-
没有。在组件本身中订阅
product,同时计算图像并将其存储在数组中。在模板上只需遍历数组。 -
好的,我明天会尝试并卷土重来@NicholasK(取决于我的水平:))
标签: angular typescript observable subscribe ionic5