Created
April 27, 2017 12:44
-
-
Save cgatian/2eab986b57d5dac2d64ed135c8e32c8f to your computer and use it in GitHub Desktop.
Image Carousel Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Input } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/interval'; | |
import 'rxjs/add/observable/from'; | |
import 'rxjs/add/operator/zip'; | |
import 'rxjs/add/operator/repeat'; | |
@Component({ | |
selector: 'image-carousel', | |
template: ` | |
<img [src]="imageSources$ | async" /> | |
` | |
}) | |
export class ImageCarouselComponent implements OnInit { | |
@Input() imageSources: string[]; | |
@Input() delayInMs = 1000; // Default to one second | |
imageSources$: Observable<string>; | |
constructor() { | |
} | |
ngOnInit() { | |
if (!(this.imageSources instanceof Array)) { | |
throw new Error('ImageComponent "imageSources" is required'); | |
} | |
this.imageSources$ = Observable.from(this.imageSources) | |
.zip( | |
Observable.interval(this.delayInMs), | |
(v) => v | |
) | |
.repeat() | |
.startWith(this.imageSources[0]); | |
this.imageSources$.subscribe(val => console.log(val)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment