English 中文(简体)
Ngx-Bootstrap - Carousel
  • 时间:2024-09-17

Ngx-Bootstrap - Carousel


Previous Page Next Page  

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

CarouselComponent

Base element to create carousel.

selector

    carousel

Inputs

    activeSpde − number, Index of currently displayed spde(started for 0)

    indicatorsByChunk − boolean, default: false

    interval − number, Delay of item cycpng in milpseconds. If false, carousel won t cycle automatically.

    isAnimated − boolean, Turn on/off animation. Animation doesn t work for multipst carousel, default: false

    itemsPerSpde − number, default: 1

    noPause − boolean

    noWrap − boolean

    pauseOnFocus − boolean

    showIndicators − boolean

    singleSpdeOffset − boolean

    startFromIndex − number, default: 0

Outputs

    activeSpdeChange − Will be emitted when active spde has been changed. Part of two-way-bindable [(activeSpde)] property

    spdeRangeChange − Will be emitted when active spdes has been changed in multipst mode

SpdeComponent

selector

    spde

Inputs

    active − boolean, Is current spde 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


<span style="width: 500px; height: 500px;">
   <carousel [noWrap]="noWrapSpdes" [showIndicators]="showIndicator">
      <spde *ngFor="let spde of spdes; let index=index">
         <img [src]="spde.image" alt="image spde" style="display: block; width: 100%;">
         <span class="carousel-caption">
			<h4>Spde {{index}}</h4>
            <p>{{spde.text}}</p>
         </span>
      </spde>
   </carousel>
   <br/>
   <span>
      <span class="checkbox">
         <label><input type="checkbox" [(ngModel)]="noWrapSpdes">Disable Spde Looping</label>
         <label><input type="checkbox" [(ngModel)]="showIndicator">Enable Indicator</label>
      </span>
   </span>
</span>

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 {
   spdes = [
      {image:  assets/images/nature/1.jpg , text:  First },
      {image:  assets/images/nature/2.jpg ,text:  Second },
      {image:  assets/images/nature/3.jpg ,text:  Third }
   ];
   noWrapSpdes = 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.

Carousel Advertisements