Angular 12 Bootstrap 4 Responsive Carousel Slider Tutorial

Bootstrap carousel slider with multiple items in angular 12. In this tutorial, i am going to show you how to intsall bootstrap 4 and create responsive carousel slider with multiple in angular 12 app.

The goal of ng-bootstrap is to completely replace JavaScript implementation for components. Nor should you include other dependencies like jQuery or popper. js. It is not necessary and might interfere with ng-bootstrap code.

How to Create Responsive Carousel Slider In Angular 12 with Bootstrap 4

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

Step 1 – Create New Angular App

Execute the following command on terminal to install angular app:

ng new my-new-app

Step 2 – Install bootstrap Carousel Slider Package

Execute the following commands on terminal to Install NPM package called ng-bootstrap/ng-bootstrap for implement bootstrap carousel in angular:

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

Step 3 – Import Modules on App.Module.ts File

Import modules; so 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 { }

Step 4 – Add Code on View File

Create bootstrap carousel slider in angular app. So, visit src/app/ and app.component.html and update the following code into it:

<div class="container-fluid">
  
<h1>Angular 12 Bootstrap Carousel Example - lara.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

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

ng serve

Recommended Angular Tutorials

Leave a Comment