0

i am newbie in angular2 and now i am trying use owl carousel

i found this tutorial but its give me many errors and i could figure out what's a problem,so can someone explain again.thanks

How to use owl-carousel in Angular2?

this is my code

Home Component html

<owl-carousel [options]="{navigation: false, pagination: true, rewindNav : false}">
   <div *ngFor="let img of images">
     <img src="http://lorempixel.com/400/200/{{img}}"/>
   </div>
</owl-carousel>

Home Component ts

import { Component, OnInit } from '@angular/core';
import { OwlCarousel } from '../owl-carousel.component';


@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

constructor() { }

ngOnInit() {
 }
 images: Array<string> = ['sports', 'abstract', 'people', 'transport',      'city', 'technics', 'nightlife', 'animals'];
}

i also imported in app.module.ts

owl.component.ts

import { Component, Input, ElementRef, HostBinding } from '@angular/core';
import * as $ from 'jquery';

 import 'owl-carousel';

  @Component({
 selector: 'owl-carousel',
  template: `<ng-content></ng-content>`
 })
 export class OwlCarousel {
 @HostBinding('class') defaultClass = 'owl-carousel';
 @Input() options: object;

  $owlElement: any;

  defaultOptions: any = {};

  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
   // use default - empty
   // for (var key in this.options) {
   //   this.defaultOptions[key] = this.options[key];
    // }
   this.$owlElement =        $(this.el.nativeElement).owlCarousel(this.defaultOptions);
}

  ngOnDestroy() {
   this.$owlElement.data('owlCarousel').destroy();
   this.$owlElement = null;
  }
 }

errors

Cannot find module 'jquery'. Module not found: Error: Can't resolve 'owl-carousel'

Community
  • 1
  • 1
lekinio
  • 127
  • 3
  • 12

1 Answers1

-1

I would suggest using something like ng2-bootstrap rather than using jQuery for this:

Long story short:

  1. install it

    npm install ng2-bootstrap
    
  2. Import the carousel module:

    import { CarouselModule } from 'ng2-bootstrap';
    
    @NgModule({
       imports:      [ CarouselModule, ...],
       declarations: [ AppComponent, ... ],
       bootstrap:    [ AppComponent ]
    }) 
    
  3. Use it:

    <carousel>
       <slide>
         <img src="https://i.ytimg.com/vi/qh7LLydY8eo/maxresdefault.jpg" alt="First slide">
       </slide>
       <slide>
         <img src="http://coolwildlife.com/wp-content/uploads/galleries/post-3004/Fox%20Picture%20003.jpg" alt="Second slide">
       </slide>
       <slide>
         <img src="https://wallpaperbrowse.com/media/images/81.jpg" alt="Third slide">
       </slide>
     </carousel>
    

And here is a working progress:

https://plnkr.co/edit/1NMmcUW1KDnxApnVUz3P?p=preview

Yaser
  • 5,609
  • 1
  • 15
  • 27