Ngx-Bootstrap - Carousel

ngx-bootstrap Carousel viene utilizzato per creare presentazioni di immagini o testo

CarouselComponent

Elemento base per creare giostra.

selettore

  • carousel

Ingressi

  • activeSlide - numero, indice della diapositiva attualmente visualizzata (iniziato per 0)

  • indicatorsByChunk - booleano, predefinito: false

  • interval- numero, ritardo dell'elemento ciclico in millisecondi. Se falso, il carosello non verrà eseguito automaticamente.

  • isAnimated- booleano, attiva / disattiva l'animazione. L'animazione non funziona per il carousel multilist, impostazione predefinita: false

  • itemsPerSlide - numero, predefinito: 1

  • noPause - booleano

  • noWrap - booleano

  • pauseOnFocus - booleano

  • showIndicators - booleano

  • singleSlideOffset - booleano

  • startFromIndex - numero, predefinito: 0

Uscite

  • activeSlideChange- Verrà emesso quando la diapositiva attiva è stata modificata. Parte della proprietà [(activeSlide)] associabile a due vie

  • slideRangeChange - Verrà emesso quando le diapositive attive sono state modificate in modalità multilist

SlideComponent

selettore

  • slide

Ingressi

  • active - booleano, la diapositiva corrente è attiva

Esempio

Dato che useremo il carousel, dobbiamo aggiornare app.module.ts usato nel capitolo Pulsanti ngx-bootstrap da usareCarouselModule.

Aggiorna app.module.ts per utilizzare 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 { }

Aggiorna test.component.html per utilizzare il 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>

Aggiorna test.component.ts per le variabili e i metodi corrispondenti.

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

Costruisci e servi

Eseguire il seguente comando per avviare il server angolare.

ng serve

Una volta che il server è attivo e funzionante. Apri http: // localhost: 4200 e verifica il seguente output.