0

I'm loading an image from a S3 bucket in my Ionic view like this:

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

The image name I want to load is "cp1.jpg", and the client?.id value 1 is taken from the component script:

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

I'm also using the "?" operator in client?.id because if I don't, I get an "id undefined" error and the page doesn't even load.

However, I'm getting a canceled request on my Network inspector, which is trying to retrieve the image before the "1" value is concatenated in the image name:

enter image description here

Right after that error, the "1" value is provided to the view and the image is loaded. But I'd like to not get that undesired retrieving try.

How can I properly load and bind the client id to get the image name right, and also to avoid getting that canceled request error?

Nelio Alves
  • 1,231
  • 13
  • 34

1 Answers1

0

In your html:

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

In your .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>
  }
}
AbM
  • 7,326
  • 2
  • 25
  • 28