I'm struggling with loading an image because the method getUserProfileImage()
is getting triggered multiple times within a loop. Is there a way to ensure the method is only called once during each iteration? I understand that this issue is related to Angular's change detection strategy, but how can I effectively address it?
I've done some research on this topic and found several solutions involving hacks to stop change detection strategy, but I prefer not to resort to such workarounds.
<div *ngFor="let user of listOfContacts">
<img class="rounded profile-thumbnail ptr" [src]="getUserProfileImage(user.UserName, user.UserId)" id="image-id-{{user.UserId}}" [routerLink]="['/community/userProfile', '0', user.UserId]">
</div>
getUserProfileImage() method located in ____.ts class
getUserProfileImage(userName: string, itemId: number){
let loadImageSub = this.util.getUserProfileImageByUserName(userName).subscribe(contactsObj => {
let imageUrl = this.toUrl(contactsObj["Images"][0].SrcAttr);
console.log("contact profile image", imageUrl);
(<HTMLInputElement>document.getElementById("image-id-" + itemId)).src = imageUrl;
loadImageSub.unsubscribe();
});
}
Error: The same image is being loaded repeatedly, causing recursive function calls. https://i.sstatic.net/xDIFD.png