I'm currently working on an Angular CLI Project
"@angular/cli": "^1.1.1",
"@angular/compiler-cli": "^4.0.0",
In order to dynamically load images from the assets folder within an ng-for loop, I am using the following line of code:
[ngStyle]="{'background-image': 'url(\'' + getPackageImage(package) + '\')'}
This functionality is implemented in the Component.ts file:
public getPackageImage(tPackage: any) {
return this.tpService.getImageFor(tPackage);
}
And the service responsible for fetching the image path is located in Service.ts:
public getImageFor(tPackage: TourPackage) {
return "/assets/images/tour-packages/" + tPackage.name + "/image.jpg";
}
While the images are loading correctly, the page tends to slow down as the number of images increases.
Each time I scroll, the getPackageImage
function is called multiple times which impacts the performance of the UI. Is there a solution to optimize this process?
Interestingly, hardcoding the image URLs directly in the CSS results in better page performance.
To showcase the issue, I have temporarily hosted the project on Firebase - Firebase link demonstrating the problem