5

I have been doing a website in Angular, and I need to load all images before showing it. I tried to find a way to make a preloader view that would be shown until all the images located in the /assets folder are loaded, but I could not find anything.

How can I do it properly in Angular?

1 Answer 1

11

I can give you 2 options to accomplish (Assuming that you have list of image urls):

First way

images = ["IMG_1_SRC","IMG_2_SRC"];
loaded = 0;

loadImages(){
  for(let i = 0; i < this.images.length; i++){
    let img = new Image();
    img.onload = () => {
      this.loaded();
    }
    img.src = this.images[i];
  }
}

loaded(){
  this.loaded++;
  if(this.images.length == this.loaded){
    //all images loaded
  }
}

Second way

images = ["IMG_1_SRC","IMG_2_SRC"];
loaded = 0;

<img hidden *ngFor="let img of images;" [src]="img" (load)="loaded()" />

loaded(){
  this.loaded++;
  if(this.images.length == this.loaded){
    //all images loaded
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

let img = new Image(); img.src = images[i];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.