Ngx-Bootstrap – Carousel

Ngx-Bootstrap - Carousel

In this guide we will discuss Ngx-Bootstrap – Carousel

ngx-bootstrap Carousel is used to create slide show of images or text

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 emitted when active slide has been changed. Part of two-way-bindable [(activeSlide)] property
  • slideRangeChange − Will be emitted when active slides has been changed in multilist mode

SlideComponent

selector

  • slide

Inputs

  • active − boolean, Is current slide active

Example

As we’re going to use carousel, We’ve to update app.module.ts used in ngx-bootstrap Buttons chapter to use CarouselModule.

Update app.module.ts to use the CarouselModule.

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

Once server is up and running. Open http://localhost:4200 and verify the following output.

Ngx-Bootstrap - Carousel

Next Topic: Click Here

Leave a Reply