Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Carousel

Ngx Bootstrap Carousel

Ngx Bootstrap Carousel

The ngx-bootstrap Carousel is used to create images or text slide shows.

CarouselComponent

Base element to create carousel.

selector

  • carousel

Inputs

  • activeSlide − number, Index of currently displayed slide(started for 0)
  • indicatorsByChunk − boolean, default: false
  • interval − number, Delay of item cycling in milliseconds. If false, carousel won't cycle automatically.
  • isAnimated − boolean, Turn on/off animation. Animation doesn't work for multilist carousel, default: false
  • itemsPerSlide − number, default: 1
  • noPause − boolean
  • noWrap − boolean
  • pauseOnFocus − boolean
  • showIndicators − boolean
  • singleSlideOffset − boolean
  • startFromIndex − number, default: 0

Outputs

  • ActiveSlideChange will be released until the active slide has been changed. Part of the two-way-bindable property [(activeSlide)]
  • SlideRangeChange will be released in multilist mode when active slides have been changed.

SlideComponent

selector

  • slide

Inputs

  • active − boolean, Is current slide active

Example

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 {
   }
}

Build and Serve

Run the following command to start the angular server.

ng serve

No Sidebar ads