angular - base64 图像以角度显示 src 中的对象-对象?
问题描述
//Pipe code where we wil manage base64
import { HttpClient } from '@angular/common/http';
import { Pipe, PipeTransform } from '@angular/core';
import { ItemMaster } from '@cust-custap/core/http/item-master/item-master.service';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'getImageUrl',
})
export class GetImageUrlPipe implements PipeTransform {
constructor(
private httpClient: HttpClient,
private itemMasterService :ItemMaster,
public sanitizer:DomSanitizer ) {}
transform(value: any, id: any): any {
console.log(id);
return this.itemMasterService.getItemImage(id).subscribe((data:any)=>{
let img=this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64,'+data);
return img;
});
}
}
<!-- HTML part-->
<div *ngFor="let product_catalog of result; let i= index;">
<img class="card-img img-fluid" width="75" height="75" alt=""
[src]="'' | getImageUrl: product_catalog.ID">
</div>
我想在 html 中显示 base64 图像,我使用管道显示 base64 图像,管道也正常工作,但图像在控制台中显示对象-对象,我已经使用 this.sanitizer.bypassSecurityTrustResourceUrl 但它不工作。请检查下面我的代码,让我在这里做错了什么?
解决方案
您看到[object object]
是因为管道正在返回订阅,而不是 base64 字符串。
你需要在AsyncPipe
这里使用。
您可以从管道的transform()
方法中返回一个 observable,而不是返回订阅。
transform(value: any): Observable {
return this.itemMasterService.getItemImage(value)
.map(data => this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64,' + data));
}
HTML部分可能是这样的
<div *ngFor="let product_catalog of result; let i= index;">
<img class="card-img img-fluid" width="75" height="75" alt=""
[src]="product_catalog.ID | getImageUrl | async">
</div>
推荐阅读
- excel - 匹配两个工作簿中的数据并复制特定单元格
- ios - 无法使用“AdIntegrator *const __strong”类型的左值初始化“UIViewController * _Nonnull”类型的参数
- mongodb - 如何使用聚合检查 MongoDB 数据库中的所有文档以获取特定字段的最新值?
- git - Git - 从 master 获取更改并将它们放在我的分支上
- javascript - 如何通过 JavaScript 中的正则表达式拆分字符串?
- java - 当我尝试将具有休眠功能的字段插入表时出错
- ios - Audiokit 合并两个转换后的文件给出 com.apple.coreaudio.avfaudio Code=-50
- powershell - 在 PowerShell 中对两个整数的集合进行排序
- node.js - Angular 8 Express Post 引发 COR 错误
- sql-server - 删除未使用的数据库后如何减小 SQL Server docker 卷的大小?