Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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];
- }
- }
Add Comment
Please, Sign In to add comment