In this tutorial, I am going to show you how to integrate Owl Carousel 2 in your Angular app. The ngx-owl-carousel-o library is going to help us create an Image slider in Angular. You can see a preview of this slider from here.
Prerequisites
Angular CLI installed on our system
To continue with this tutorial you should have installed Angular CLI in your pc. If not run below command in your terminal
1
npm install -g @angular/cli
Create Angular Project
I am going to implement this carousel in a new project. So, to create new Angular project run below command in your terminal
1
ng new angular-owl-carousel-example
Now get in to the newly created Angualr application by running below command in your terminal:
1 2
cd angular-owl-carousel-example
Start your app in the browser with below command:
1
ng serve --open
Install Owl Carousel Plugin
Now we need to install the owl carousel package in Angular application. So head over to your command prompt and execute the following command:
1
npm install ngx-owl-carousel-o
Add Carousel Module & BrowserAnimations Module
In order to bring Owl Carousel in action in our Angular application, we need to import and register CarouselModule and BrowserAnimationsModule services in the main app module class.
CarouselModeule allow you to use the Owl Carousel in Angular whereas BrowserAnimationsModule profoundly enables the animation service for you.
Next we need to add carousel styles to the styles arrays. To add it, Open the angular.json file and move to styles array and add the following owl carousel styling CSS paths
Prevously I showed you a simple example. Let’s see a bit of an advanced example. We will use *ngFor directive to generate slides in angular dynamically.
To create our Slides Dymnamic, we need to refactor our app.component.ts according to below way.
In this tutorial, I showed you how to intergrade Owl Carousel in Angular. To knowing more about the owl carousel, you can check out their documentation here. You can find the source code of this application from here.