2

My products api returns products info which includes image name to download asynchronously.

{products: 
 [
  {name: 'test product 1', price: 'xxxx', image: 'product1.jpg' etc..}
  {name: 'test product 2', price: 'xxxx', image: 'product2.jpg' etc..}
 ]
}

I need to make a GET call with image name as query param which returns base 64 string of image.

GET api: https://mydomain/api/img?name=product1.jpg
response : 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/7QBGUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAACk...'

I want to show the product on page and then load the image asynchronously (lazy load).

<div *ngFor='let product of products | async'>
    <img [src]='getImage(product.image)' alt='loading...'/>
    <p>{{product.name}}<p>
    <span>{{product.price}}</span>
</div>

  getImage(name: string) {
    return this.appService
      .getImageData(name)
      .pipe(switchMap((res) => res))
      .subscribe(
        (res) => res, // This will return the base64 string
        (err) => err
      );
  }

Can anyone help to get the lazily loaded images with base64 string in Angular application?

0 Answers0