Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: Carousel next and prev

itay pro asked 4 years ago


How do you get the handle of a Carousel element in order to use previousSlide() and nextSlide() methods ?


Konrad Stępień staff answered 4 years ago


Try my code:

add #carousel to your HTML and button with functions.

<mdb-carousel #carousel [isControls]="false" [animation]="'slide'">
  <mdb-carousel-item>
    <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(130).jpg" alt="First slide">
  </mdb-carousel-item>
  <mdb-carousel-item>
    <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(129).jpg" alt="Second slide">
  </mdb-carousel-item>
  <mdb-carousel-item>
    <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(70).jpg" alt="Third slide">
  </mdb-carousel-item>
</mdb-carousel>

<button type="button" mdbBtn color="primary" mdbWavesEffect (click)="prev()">Prev</button>
<button type="button" mdbBtn color="primary" mdbWavesEffect (click)="next()">Next</button>

and then use @ViewChild in TS file:

import { Component, ViewChild } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  @ViewChild('carousel', {static: false}) carousel: any;


  next() {
    this.carousel.nextSlide();
  }
  prev() {
    this.carousel.previousSlide();
  }
}

We will add this example for our documentation page.

Best, Konrad.


itay pro commented 4 years ago

Excellent.......



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: 8.8.2
  • Device: NA
  • Browser: NA
  • OS: NA
  • Provided sample code: Yes
  • Provided link: No