【问题标题】:Placeholder image Angular占位符图片 Angular
【发布时间】:2022-01-27 07:35:01
【问题描述】:

我有以下问题:我想使用默认图像,直到通过异步调用加载另一个图像。问题是,遵循此How do I get a placeholder image to load when my image is still loading from server 并使用此代码我得到两个图像,而不是一个替换另一个。我应该修改什么?

  async getData() {
     await this.gs.getMaxGifs(this.name, this.offset).subscribe((response: any) => {
       this.result.push(...response.data);
       if (this.value == 'asc') {
         this.result.sort((val1, val2) => {
           return new Date(val1.import_datetime).getTime() - new Date(val2.import_datetime).getTime()  //ASC
         })
       }
       else {
         this.result.sort((val1, val2) => {
           return new Date(val2.import_datetime).getTime() - new Date(val1.import_datetime).getTime()  //DESC
         })
       }
       this.offset += 50;
       if (this.result.length != response.pagination.total_count) {
          this.getData()
       }
     });
   }

.html 页面:

<ion-grid fixed>
  <ion-row>
    <ion-col size="4" *ngFor="let r of result; let i = index">
      <ion-card>
        <ion-card-content>
          <img #userImage src={{r.images.downsized.url}} width="300" height="200" (click)="getDetails(i)">
          <img *ngIf="!userImage.complete" src="../../../assets/error.png">
        </ion-card-content>
        <ion-card-header *ngIf="clicked === i">
          <ion-card-title>
            title: {{r?.title}}
          </ion-card-title>
          <ion-card-subtitle>username: {{r?.username}}</ion-card-subtitle>
          <ion-card-subtitle>import date:{{r?.import_datetime}}</ion-card-subtitle>
        </ion-card-header>
      </ion-card>
    </ion-col>
  </ion-row>
</ion-grid>

【问题讨论】:

  • 在加载之前隐藏图像可能会更好,但如果您绝对需要占位符图像,您可以将其作为 Base64 表示形式嵌入到代码库中。

标签: angular ionic-framework placeholder


【解决方案1】:

也许您选择的方法在 ionic 中不起作用。

试试这个:

 <ion-card-content>

      <ng-container *ngIf="r && r?.images?.downsized?.url != null; else elseBlockErrorImg">
         <img src={{r.images.downsized.url}} width="300" height="200" (click)="getDetails(i)">
      </ng-container>

      <ng-template  #elseBlockErrorImg>
          <img src="/assets/error.png">                                
      </ng-template>

</ion-card-content>

我不知道你是如何得到 'r' 的,但如果你是异步得到的,试试改变这个:

*ngIf="r | async; else elseBlockErrorImg">

【讨论】:

  • 嗨@juan,谢谢你的回答,但它是一样的。上传两张图片
  • 嗨@sirss,我刚刚编辑了我的答案。试试这些 *ngIf 的新方法(我现在给了你 2 种方法),祝你好运!为了获得更好的帮助,您应该展示您的代码如何获得“r”(它来自哪里,类型等)
  • Nada =( ,有了这些新更改,您将不再看到占位符图像。我通过添加获取数据的方式修改了帖子
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 2021-09-18
  • 2020-09-07
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多