Angular 11/12 OWl Carousel Tutorial Example

Owl Carousel Example Angular 11/12. In this tutorial, we will explain how to use Owl Carousel in the Angular 11/12 app using the ngx-owl-carousel-o package.

This Angular tutorial is compatible with version 4+ including latest version 12, 11, 10, 9, 8 ,7, 6 & 5.

Owl Carousel Tutorial Example Angular 11/12

We simply need to follow the given steps for implementing the owl carousel in the Angular 12 app:

 

Step 1 is to Create the New Angular App

Step 2 is to Install the Owl Carousel Library

Step 3 is to Add Code on App.Module.ts File

Step 4 is to Add Code on View File

Step 5 is to Add Code On app.Component ts File

Step 6 is to Start the Angular App

 

Now, Let’s Learn in Detail

Step 1- Creating the New Angular App

First of all, open your terminal and execute the given command on it to install an angular app:

ng new owl-carousel-app

Step 2 : Installing the Owl Carousel Library

Next, just install the NPM package ngx-owl-carousel -save for implementing the owl carousel in the Angular app. Further, we can install the packages by executing the below-mentioned commands on the terminal:

npm install jquery --save
npm install ngx-owl-carousel-o

Then, we have to open the angular.json file and update the below code into it:

...
"styles": [
    "node_modules/ngx-owl-carousel-o/lib/styles/prebuilt-themes/owl.carousel.min.css",
    "node_modules/ngx-owl-carousel-o/lib/styles/prebuilt-themes/owl.theme.default.min.css",
],
"scripts": [
  "node_modules/jquery/dist/jquery.min.js",
]
....

Step 3: Adding Code on App.Module.ts File

Afterward, we visit the src/app directory and open the app.module.ts file. Thereafter, we also add the given lines into the app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
   
import { AppComponent } from './app.component';
import { CarouselModule } from 'ngx-owl-carousel-o';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
   
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CarouselModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 4: Adding the Code on View File

In the next step, create the owl carousel in the Angular app. Then, we visit src/app/ and app.component.html after which, we update the following code into it:

<h1>Angular 11 Owl Carousel Integration Tutorial - Thecodemon.com</h1>
   
<owl-carousel-o [options]="customOptions">
   
<ng-container *ngFor="let slide of slides">
  <ng-template carouselSlide [id]="slide.id">
    <img [src]="slide.img" >
  </ng-template>
</ng-container>
   
</owl-carousel-o>

Step 5: Adding Code On app.Component ts File

Now, follow this step in which we visit the src/app directory and open app.component.ts.  After this, we add the following code into component.ts file:

import { Component } from '@angular/core';
import { OwlOptions } from 'ngx-owl-carousel-o';
   
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ng-carousel-demo';
   
  customOptions: OwlOptions = {
    loop: true,
    mouseDrag: false,
    touchDrag: false,
    pullDrag: false,
    dots: false,
    navSpeed: 700,
    navText: ['', ''],
    responsive: {
      0: {
        items: 1
      },
      400: {
        items: 2
      },
      740: {
        items: 3
      },
      940: {
        items: 4
      }
    },
    nav: true
  }
   
    slides = [
      {id: 1, img: "https://dummyimage.com/350x150/423b42/fff"},
      {id: 2, img: "https://dummyimage.com/350x150/2a2b7a/fff"},
      {id: 3, img: "https://dummyimage.com/350x150/1a2b7a/fff"},
      {id: 4, img: "https://dummyimage.com/350x150/7a2b7a/fff"},
      {id: 5, img: "https://dummyimage.com/350x150/9a2b7a/fff"},
      {id: 6, img: "https://dummyimage.com/350x150/5a2b7a/fff"},
      {id: 6, img: "https://dummyimage.com/350x150/4a2b7a/fff"}
    ];
}

Step 6: Starting the Angular App

Lastly, we reach the final step of implementation where we execute the following command on the terminal to start the slick carousel in the angular app:

ng serve

Conclusion

Finally, we reach the end of using the owl carousel in the Angular 11/12 tutorial. In this tutorial, we have given a simple example with the help of which you can easily use the owl carousel yourself in your Angular app. 

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *