Last active
June 24, 2018 14:58
-
-
Save realtomaszkula/54050c52ded6474c73afd0d88b58ae39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@Directive({ | |
selector: '[carousel]' | |
}) | |
export class CarouselDirective implements OnInit { | |
context: CarouselContext | null = null; | |
index = 0; | |
constructor( | |
private tpl: TemplateRef<CarouselContext>, | |
private vcr: ViewContainerRef | |
) {} | |
@Input('carouselFrom') images: string[]; | |
ngOnInit(): void { | |
this.context = { | |
$implicit: this.images[0], | |
controller: { | |
next: () => this.next(), | |
prev: () => this.prev() | |
} | |
}; | |
this.vcr.createEmbeddedView(this.tpl, this.context); | |
} | |
next() { | |
this.index++; | |
if (this.index >= this.images.length) { | |
this.index = 0; | |
} | |
this.context.$implicit = this.images[this.index]; | |
} | |
prev() { | |
this.index--; | |
if (this.index < 0) { | |
this.index = this.images.length - 1; | |
} | |
this.context.$implicit = this.images[this.index]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment