【问题标题】:How to properly load and bind a parameter to a img url using Ionic 3 / Angular 5?如何使用 Ionic 3 / Angular 5 正确加载参数并将其绑定到 img url?
【发布时间】:2017-12-18 16:06:47
【问题描述】:

我正在 Ionic 视图中从 S3 存储桶加载图像,如下所示:

<ion-avatar>
  <img class="circle" src="https://s3-sa-east-1.amazonaws.com/mybucket/cp{{client?.id}}.jpg">
</ion-avatar>

我要加载的图片名称是“cp1.jpg”,client?.id值1取自组件脚本:

ionViewDidLoad() {
  let email = this.storage.getLocalUser().email;
  this.clientService.findByEmail(email)
    .subscribe(resp => {
      this.client = resp;
    },
    error => {
      console.log(error);
    });
}

我也在使用“?”客户端中的操作员?

但是,我的网络检查器收到了一个已取消的请求,它试图在图像名称中连接“1”值之前检索图像:

在该错误之后,“1”值被提供给视图并加载图像。但我不想进行那种不受欢迎的检索尝试。

如何正确加载和绑定客户端 ID 以获取正确的图像名称,并避免出现取消请求错误?

【问题讨论】:

  • 你可以有*ngIf="client?.id"

标签: javascript angular ionic-framework


【解决方案1】:

在您的 html 中:

<ion-avatar>
  <img class="circle" [src]="avatar_for_client()">
</ion-avatar>

在你的.ts:

avatar_for_client(){
  if (this.client){
    return `https://s3-sa-east-1.amazonaws.com/mybucket/cp${this.client.id}.jpg`
  }else{
    return <some placeholder image url>
  }
}

【讨论】:

  • 这不是avatar_for_client()吗?
  • 工作感谢!另一个小修复:${this.client.id}。顺便说一句,如果存储桶图像不存在,我将如何返回占位符图像 url?谢谢。
  • @NelioAlves 你可以收听(error)事件stackoverflow.com/a/36430121/2697183
猜你喜欢
  • 2021-05-03
  • 2019-10-26
  • 2018-04-28
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2019-02-02
  • 2018-06-02
相关资源
最近更新 更多