Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import { Component, OnInit, Input } from '@angular/core';
  2. import { Observable } from 'rxjs/Observable';
  3. import 'rxjs/add/observable/interval';
  4. import 'rxjs/add/observable/from';
  5. import 'rxjs/add/operator/zip';
  6. import 'rxjs/add/operator/repeat';
  7.  
  8. @Component({
  9. selector: 'image-carousel',
  10. template: `
  11. <img [src]="imageSources$ | async" />
  12. `
  13. })
  14. export class ImageCarouselComponent implements OnInit {
  15. @Input() imageSources: string[];
  16. @Input() delayInMs = 1000; // Default to one second
  17.  
  18. imageSources$: Observable<string>;
  19.  
  20. constructor() {
  21.  
  22. }
  23.  
  24. ngOnInit() {
  25. if (!(this.imageSources instanceof Array)) {
  26. throw new Error('ImageComponent "imageSources" is required');
  27. }
  28. this.imageSources$ = Observable.from(this.imageSources)
  29. .zip(
  30. Observable.interval(this.delayInMs),
  31. (v) => v
  32. )
  33. .repeat()
  34. .startWith(this.imageSources[0]);
  35.  
  36. this.imageSources$.subscribe(val => console.log(val));
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement