【问题标题】:background image not showing in Angular背景图像未在 Angular 中显示
【发布时间】:2020-02-05 23:48:13
【问题描述】:

是的,我搜索并找到了很多示例,但没有一个对我有用。我不确定我做错了什么。需要帮助!

如果我输入<img> 标签,我可以看到图像,但当我输入background-image 时就看不到。但是当我检查元素时,我可以看到二进制图像。

尝试在 stackblitz 中做一个示例 https://stackblitz.com/edit/display-image-from-rest-api-qpnspq?embed=1&file=src/app/app.component.ts

HTML:

 <!-- This works -->
 <div class="row" style="max-width: 100%; margin-left: 1px;" >
     <img id="dashboardImage" [src]='imageBlobDataUrl' height='500' width='500' />
 </div>

 <!-- This does NOT works -->
<div class="row" style="max-width: 100%; margin-left: 1px;" [style.background-image]="image"> </div>

TS:

    @Input() imageBlobDataUrl: any;
    selectedImage: QiImage = new QiImage();

    public header:SafeHtml;
    public content:SafeHtml[];
    public image:SafeStyle;

  constructor(
          private qiImageService: QiImageService,
          private sanitizer: DomSanitizer,
          private imageService: ImageService
  ) {}
  populateImageDetails(lineId, bitmapFileName) {
      this.imageService
          .populateImageDetails(lineId, bitmapFileName)
              .subscribe( (selectedImage : any) => {
                  this.selectedImage = selectedImage;
                  //let TYPED_ARRAY = new Uint8Array(selectedImage.imageData);
                  //const STRING_CHAR = String.fromCharCode.apply(null, TYPED_ARRAY);
                  //let base64String = btoa(STRING_CHAR);
                  let objectURL = 'data:image/png;base64,' + selectedImage.imageData;
                  this.imageBlobDataUrl = this.sanitizer.bypassSecurityTrustUrl(objectURL);
                  this.image = this.sanitizer.bypassSecurityTrustStyle(`url(${objectURL})`);
              });
  }

【问题讨论】:

  • 请添加一个 stackblitz 示例以显示您尝试过的代码。
  • 设置imageblobdataurl为图片然后勾选
  • @nircraft - 添加了 stackblitz 网址
  • @HitechHitesh - 抱歉,没听懂你在说什么
  • @SK。添加了我的答案

标签: css angular typescript sanitize


【解决方案1】:

当我尝试您的 stackblitz 时,它工作正常。您需要修复 CSS 以使图像显示在视图中。

不要将bypassSecurityTrustUrl 用于样式网址。所以下面的缩略图是无效的:

this.thumbnail = this.sanitizer.bypassSecurityTrustUrl(objectURL); // Do NOT Use

this.image = this.sanitizer.bypassSecurityTrustStyle(`url(${objectURL})`); // This works fine and is Valid

尝试为您的图片调整 CSS 位置,它会显示出来:

<div class="row" 
  style="width: 100%; height: 100%; position:absolute; 
  margin-left: 1px;  background-repeat: no-repeat;" 
  [style.background-image]="image"> </div>

【讨论】:

  • 天啊!!!从过去 2 天开始,我一直在研究这个问题。感谢您指出了这一点。宽度:100%;高度:100%;是我错过的东西。
  • @SK。每个人都会发生..玩得开心:)
【解决方案2】:

如果您尝试使用 CSS 设置背景图像?像这样:

.bg {
  background-image: url("../../yourimage");
  height: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

}

在模板中你可以使用绑定来设置你喜欢的css类

【讨论】:

  • 我的图像作为 BLOB(二进制)数据来自 rest 服务,而不是 png/jpg 文件路径。我也尝试过使用 mnau 的不同方式。
  • 如果你想设置一个带有背景图片的 div,你需要在 CSS 中设置,你可以使用一个函数动态地完成它,它会接收一个 url 图片并通过属性投标来设置它。看看这个文档Template biding
  • 我强烈推荐 tour of heroes 练习,以便更好地理解 Angular 基础资源。我总是看看
【解决方案3】:

你可以把 URL 图片放在 src 上,即使它是在 base64 上的

 <img id="dashboardImage" [src]='objectURL' height='500' width='500' />

【讨论】:

  • 我已经说过,如果我将图像放在 标签中,它可以工作,但我希望它作为背景图像
猜你喜欢
  • 2019-02-11
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多