Skip to content

Commit d57f395

Browse files
guillenotfoundihadeed
authored andcommitted
fix(): use fallback image if image fails to load (#6)
1 parent e2616c3 commit d57f395

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/components/img-loader/img-loader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export class ImgLoader implements OnInit {
118118
}
119119

120120
this.imageLoader.getImagePath(this.imageUrl)
121-
.then((imageUrl: string) => this.setImage(imageUrl));
121+
.then((imageUrl: string) => this.setImage(imageUrl))
122+
.catch((error: any) => this.setImage(this.fallbackUrl || this.imageUrl));
122123
}
123124

124125
private setImage(imageUrl: string): void {
@@ -159,7 +160,7 @@ export class ImgLoader implements OnInit {
159160
this.renderer.setElementStyle(element, 'background-repeat', this.backgroundRepeat);
160161
}
161162

162-
this.renderer.setElementStyle(element, 'background-image', 'url(\'' + imageUrl +'\')');
163+
this.renderer.setElementStyle(element, 'background-image', 'url(\'' + imageUrl + '\')');
163164
}
164165
}
165166

src/providers/image-loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ImageLoader {
5252
* @returns {Promise<string>} Returns a promise that will always resolve with an image URL
5353
*/
5454
getImagePath(imageUrl: string): Promise<string> {
55-
return new Promise<string>((resolve) => {
55+
return new Promise<string>((resolve, reject) => {
5656

5757
let getImage = () => {
5858
this.getCachedImagePath(imageUrl)
@@ -67,7 +67,7 @@ export class ImageLoader {
6767
resolve(localPath);
6868
})
6969
.catch((e) => {
70-
resolve(imageUrl);
70+
reject();
7171
this.throwError(e);
7272
});
7373
});

0 commit comments

Comments
 (0)