【发布时间】:2018-12-30 23:27:54
【问题描述】:
要在图片库中显示图片,我必须通过 REST API 下载 Blob。我有多个图像由 *ngFor 循环呈现,如下所示:
<div *ngFor="let galleryItem of galleryItems">
<img [src]="galleryItem.blobUrlThumb">
</div>
但是,当我的 Blob URL 被写入到 galleryItem 对象时,图像 src 顽固地停留在 null 并且不会更新。
// Component code example
galleryItems: GalleryItem[] = [galleryItem];
this.apiService.download(`/x/y/${galleryItem.id}`).subscribe((blob: Blob) => {
galleryItem.blobUrlThumb = this.sanitization.bypassSecurityTrustUrl(URL.createObjectURL(blob));
});
如果我对组件中的局部变量执行相同操作,一切正常:
<div *ngFor="let galleryItem of galleryItems">
<img [src]="blobUrlThumb">
</div>
我希望将 Blob URL 保存在 GalleryItem 对象中。为什么没有在所述对象上进行更新?
【问题讨论】:
-
可以分享组件代码吗?
-
您可能在收到您的 api 响应后无法检测到更改,您介意发布您的组件的那部分吗?
-
你总是在你的订阅中改变你的对象 galleryItem.blobUrlThumb,所以你的对象总是简单地改变 console.log 它。我做了同样的事情(使用局部变量)并将其添加到数组中(如果生成的 blob 不为空,则推送它)。
-
@JensHabegger 我发布了一些组件代码。我只是在其中订阅并设置
galleryItem.blobUrlThumb variable。 -
@JensHabegger,这不是变更检测的问题。更改
galleryItem.blobUrlThumb不会对用于初始化的数组产生影响。因此手动触发变更检测将没有任何价值