src/comps/imgloader/ImgLoader.ts
changeDetection | ChangeDetectionStrategy.Default |
selector | imgLoader |
template |
|
circle
|
Type:
Default value: |
Defined in src/comps/imgloader/ImgLoader.ts:30
|
defaultImage
|
Type: |
Defined in src/comps/imgloader/ImgLoader.ts:27
|
images
|
Type: |
Defined in src/comps/imgloader/ImgLoader.ts:33
|
style
|
Type: |
Defined in src/comps/imgloader/ImgLoader.ts:24
|
constructor(cdr: ChangeDetectorRef)
|
Defined in src/comps/imgloader/ImgLoader.ts:18
|
Protected getImageUrl |
getImageUrl()
|
Defined in src/comps/imgloader/ImgLoader.ts:37
|
Returns :
void
|
Protected onImageLoaded |
onImageLoaded()
|
Defined in src/comps/imgloader/ImgLoader.ts:46
|
Returns :
void
|
Protected onImageError |
onImageError()
|
Defined in src/comps/imgloader/ImgLoader.ts:50
|
Returns :
void
|
Static reloadImage |
reloadImage()
|
Defined in src/comps/imgloader/ImgLoader.ts:54
|
Returns :
void
|
Protected imageRetries |
imageRetries: |
Default value : 0
|
Defined in src/comps/imgloader/ImgLoader.ts:35
|
import {Component, ChangeDetectionStrategy, ChangeDetectorRef, Input} from '@angular/core';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/merge';
import 'rxjs/add/operator/distinctUntilChanged';
import {Lib} from "../../Lib";
@Component({
selector: 'imgLoader',
changeDetection: ChangeDetectionStrategy.Default,
template: `
<div *ngIf="defaultImage">
<img [ngStyle]="_style.img" [ngClass]="{'img-circle': circle}" [src]="getImageUrl()" (load)="onImageLoaded()" (error)="onImageError()" />
</div>
`
})
export class ImgLoader {
constructor(private cdr:ChangeDetectorRef) {
}
@Input('style')
_style:any = {};
@Input()
defaultImage:string = '';
@Input()
circle:boolean = false;
@Input()
images:Array<string> = [];
private imageRetries:number = 0;
private getImageUrl() {
if (this.images.length == 0)
return this.defaultImage;
if (this.images[this.imageRetries] == undefined)
return this.defaultImage;
var url = this.images[this.imageRetries] + (Lib.DevMode() ? '?random=xyz' : `?random=' ${Math.random()}`);
return url;
}
private onImageLoaded() {
this.cdr.detach();
}
private onImageError() {
this.imageRetries++;
}
public reloadImage(){
this.imageRetries = 0;
this.cdr.reattach();
}
}