Search code examples
angularangular7angular8

How to perform any action on image, after it loads from API


I have made an API which returns the URL of the image. Using Angular 8, I am rendering that image. Up to here it works fine.

I want to execute a function after rendering that image. I am writing that action, but it is getting executed before the image renders. Is there any way I can execute that function after the rendering of that image?

<div id="viewPng" class="uploader">
     <img id="file-image" [src]="imageUrl" alt="Preview" usemap="shapes-map">
     <map name="shapes-map">
          <area shape="rect" title="Square" coords="235, 758, 1126, 942" href="/wp-conF,<nt/uploads/square.png" target="_blank" />
     </map>
</div>
this._pvgfpService.pdfToPng(formData).subscribe(data => {
  this.imageUrl = "https://www.google.com/imgres?imgurl=https%3A%2F%2Fd2eip9sf3oo6c2.cloudfront.net%2Ftags%2Fimages%2F000%2F000%2F300%2Ffull%2Fangular2.png&imgrefurl=https%3A%2F%2Fegghead.io%2Fcourses%2Fget-started-with-angular&docid=f68td0wdokO9hM&tbnid=yBO6E6F_jZlf5M%3A&vet=10ahUKEwioiY7Mx47lAhWj6XMBHWtFC7MQMwhoKAEwAQ..i&w=800&h=800&bih=669&biw=1299&q=Angular%20Image&ved=0ahUKEwioiY7Mx47lAhWj6XMBHWtFC7MQMwhoKAEwAQ&iact=mrc&uact=";
  alert("Hello World");
});

Here I am getting the value of imageUrl, it should render the image to front end and then it should execute alert, but alert is getting executed before image renders.

I have also tried writing alert outside subscribe, but alert is still getting executed first.


Solution

  • You can use load event like

    <img id="file-image" [src]="imageUrl" alt="Preview" (load)="onImageLoad()" usemap="shapes-map">
    

    ts part

    onImageLoad(){
    alert("loaded")
    }
    

    demo