Angular 16 Bootstrap 5 Carousel Tutorial with Example

Angular 16 bootstrap carousel example; In this tutorial, i am going to show you how to use bootstrap 5 carousel in the Angular 16 apps using ng bootstrap library.

Follow the below steps to integrate bootstrap 5 carousel in Angular 16 apps; as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install bootstrap Carousel Library
  • Step 3 – Import Modules in Module.ts File
  • Step 4 – Create Carousel in View File
  • Step 5 – Add Code On app.Component ts File
  • Step 6 – Start the Angular App

Step 1 – Create New Angular App

Run the following command on terminal to install angular app:

ng new my-new-app

Then run the following command on terminal to install NPM package called ng-bootstrap/ng-bootstrap to implementing bootstrap carousel in angular apps:

npm install jquery --save
ng add @ng-bootstrap/ng-bootstrap

Step 3 – Import Modules in Module.ts File

Go to src/app directory and open app.module.ts file. And then add the following lines of into app.module.ts file:

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

Go to src/app/ and app.component.html and update the following code into it:

<div class="container-fluid">
  
<h1>Angular 16 Bootstrap Carousel Example - Laratutorials.com</h1>
  
<ngb-carousel>
  <ng-template ngbSlide *ngFor="let image of images">
    <div class="wrapper">
      <img [src]="image.src" alt="Random first slide">
    </div>
    <div class="carousel-caption">
      <h3>{{ image.title }}</h3>
      <p>{{ image.short }}</p>
    </div>
  </ng-template>
</ngb-carousel>
    
</div>

Step 5 – Add Code On app.Component ts File

Go to the src/app directory and open app.component.ts. Then add the following code into component.ts file:

import { Component } from '@angular/core';
import { NgbCarouselConfig } from '@ng-bootstrap/ng-bootstrap';
  
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NgbCarouselConfig]
})
export class AppComponent {
  title = 'ng-carousel-demo';
  
  images = [
    {title: 'First Slide', short: 'First Slide Short', src: "https://picsum.photos/id/700/900/500"},
    {title: 'Second Slide', short: 'Second Slide Short', src: "https://picsum.photos/id/1011/900/500"},
    {title: 'Third Slide', short: 'Third Slide Short', src: "https://picsum.photos/id/984/900/500"}
  ];
  
  constructor(config: NgbCarouselConfig) {
    config.interval = 2000;
    config.keyboard = true;
    config.pauseOnHover = true;
  }
}

Step 6 – Start the Angular App

Run the following command on terminal to start angular bootstrap carousel app:

ng serve

Related Tutorials

Leave a Comment