Angular 11/12 Bootstrap Carousel
Let’s learn in this tutorial step by step how to use bootstrap carousel in Angular 11/12 using ng-bootstrap/ng-bootstrap package.
Bootstrap Carousel Tutorial Example Angular 11/12
We simply need to follow the given steps for implementing the ————- in Angular 12 app:
Step 1 is to Create the New Angular App
Step 2 is to Install the Bootstrap 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 bootstrap-carousel-app
Step 2: Installing the Bootstrap Carousel Library
Next, we have to install the NPM package called ng-bootstrap/ng-bootstrap for implementation of Bootstrap Carousel in Angular app. Further, we can install the packages by executing the following commands on the terminal
npm install jquery --save
ng add @ng-bootstrap/ng-bootstrap
Step 3: Adding Code on App.Module.ts File
Afterward, we visit src/app directory and open app.module.ts file. Thereafter, we also add the following lines 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: Adding the Code on View File
In the next step, create the bootstrap carousel in the Angular app. Then, we visit src/app/ and app.component.html and update the following code into it:
<div class="container-fluid">
<h1>Angular 11 Bootstrap Carousel Example - Thecodemon.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: 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 { 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: Starting the Angular App
Lastly, we reach the final step of implementation where we execute the following command on the terminal to start the angular bootstrap carousel app:
ng serve
Conclusion
Finally, we reach the end of this tutorial in which we have explained the use of Bootstrap Carousel in Angular 11/12. In this tutorial, we have given an example of the same. Hope you have found it quite useful.
Thanks