Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. @Directive({
  2. selector: '[carousel]'
  3. })
  4. export class CarouselDirective implements OnInit {
  5. context: CarouselContext | null = null;
  6. index = 0;
  7.  
  8. constructor(
  9. private tpl: TemplateRef<CarouselContext>,
  10. private vcr: ViewContainerRef
  11. ) {}
  12.  
  13. @Input('carouselFrom') images: string[];
  14.  
  15. ngOnInit(): void {
  16. this.context = {
  17. $implicit: this.images[0],
  18. controller: {
  19. next: () => this.next(),
  20. prev: () => this.prev()
  21. }
  22. };
  23.  
  24. this.vcr.createEmbeddedView(this.tpl, this.context);
  25. }
  26.  
  27. next() {
  28. this.index++;
  29. if (this.index >= this.images.length) {
  30. this.index = 0;
  31. }
  32. this.context.$implicit = this.images[this.index];
  33. }
  34.  
  35. prev() {
  36. this.index--;
  37. if (this.index < 0) {
  38. this.index = this.images.length - 1;
  39. }
  40. this.context.$implicit = this.images[this.index];
  41. }
  42. }
Add Comment
Please, Sign In to add comment