Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Carousel
The ngx-bootstrap Carousel is used to create images or text slide shows.
Base element to create carousel.
selector
Inputs
Outputs
selector
Inputs
We have to update app.module.ts used in the ngx-bootstrap Buttons chapter to use CarouselModule as we're going to use the carousel.
In order to use the CarouselModule, update app.module.ts.
app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; import { TestComponent } from './test/test.component'; import { AccordionModule } from 'ngx-bootstrap/accordion'; import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert'; import { ButtonsModule } from 'ngx-bootstrap/buttons'; import { FormsModule } from '@angular/forms'; import { CarouselModule } from 'ngx-bootstrap/carousel'; @NgModule({ declarations: [ AppComponent, TestComponent ], imports: [ BrowserAnimationsModule, BrowserModule, AccordionModule, AlertModule, ButtonsModule, FormsModule, CarouselModule ], providers: [AlertConfig], bootstrap: [AppComponent] }) export class AppModule { }
Update test.component.html to use the Carousel.
test.component.html
<div style="width: 500px; height: 500px;"> <carousel [noWrap]="noWrapSlides" [showIndicators]="showIndicator"> <slide *ngFor="let slide of slides; let index=index"> <img [src]="slide.image" alt="image slide" style="display: block; width: 100%;"> <div class="carousel-caption"> <h4>Slide {{index}}</h4> <p>{{slide.text}}</p> </div> </slide> </carousel> <br/> <div> <div class="checkbox"> <label><input type="checkbox" [(ngModel)]="noWrapSlides">Disable Slide Looping</label> <label><input type="checkbox" [(ngModel)]="showIndicator">Enable Indicator</label> </div> </div> </div>
Update test.component.ts for corresponding variables and methods.
test.component.ts
import { Component, OnInit } from '@angular/core'; import { CarouselConfig } from 'ngx-bootstrap/carousel'; @Component({ selector: 'app-test', templateUrl: './test.component.html', providers: [ { provide: CarouselConfig, useValue: { interval: 1500, noPause: false, showIndicators: true } } ], styleUrls: ['./test.component.css'] }) export class TestComponent implements OnInit { slides = [ {image: 'assets/images/nature/1.jpg', text: 'First'}, {image: 'assets/images/nature/2.jpg',text: 'Second'}, {image: 'assets/images/nature/3.jpg',text: 'Third'} ]; noWrapSlides = false; showIndicator = true; constructor() { } ngOnInit(): void { } }
Run the following command to start the angular server.
ng serve