Rocky
Rocky

Reputation: 597

How to create carousel in angular 8 using Angular Material?

The first image is what I am getting currently and second is what I actually want

First:-

enter image description here

Second:-

enter image description here

<mat-card class="carousel-data">
    <mat-grid-list cols="2" rowHeight="30px" class="carousel-data" *ngFor="let data of resource let i = index;" (click)="showdata()" >

        <mat-grid-tile>     
           <img class="m-t-0 m-b-0 "[src]="data.img" alt="" width="24">
            <div class="prof-left" style="margin-left: 10px;">
                <p class="m-t-0 m-b-0 bank-name">{{ data.bank_name}}</p>
                <p class="m-t-0 m-b-0 bank-account">{{ data.account_number }}</p>
            </div>
        </mat-grid-tile>
        <mat-grid-tile>
            <div class="prof-right">
                <p class="m-t-0 m-b-0 total-bal">Total Bal</p>
                <p class="m-t-0 m-b-0 total-amount">{{ data.amount }}</p>
            </div>
        </mat-grid-tile>
    </mat-grid-list>
</mat-card>

enter image description here

Upvotes: 1

Views: 30046

Answers (3)

Rocky
Rocky

Reputation: 597

Steps Needs to follow to achieve Carousel:-

  • ng new owl-carousel

  • npm install ngx-owl-carousel owl.carousel jquery --save

  • Add library files to angular.json:

"styles": [
  "src/styles.css",
  "./node_modules/owl.carousel/dist/assets/owl.carousel.min.css",
  "./node_modules/owl.carousel/dist/assets/owl.theme.default.min.css"
],
"scripts": [
  "./node_modules/jquery/dist/jquery.min.js",
  "./node_modules/owl.carousel/dist/owl.carousel.min.js"
]
  • Update app.module.ts file:
import { OwlModule } from 'ngx-owl-carousel';
// Add OwlModule to imports at below section
imports: [BrowserModule, OwlModule],
  • Update app.component.ts file
mySlideImages = ['../assets/images/image1.jpg','../assets/images/image2.jpeg','../assets/images/image3.jpg'];   
mySlideOptions = {items: 1, dots: true, nav: true};
  • Update app.component.html file:
<owl-carousel [options]="mySlideOptions" [items]="images" [carouselClasses]="['owl-theme', 'sliding']" >
  <div class="item" *ngFor="let image of mySlideImages; let i = index">
    <div>
      <img src={{image}}/>
    </div>
  </div>
</owl-carousel>

For more details have a look at the Carousel demo

Upvotes: 0

Wiki
Wiki

Reputation: 222

Run yarn add ngx-owl-carousel-o or npm install ngx-owl-carousel-o or ng add ngx-owl-carousel-o.

Add styles (one of these variants). src/styles.sass or src/styles.scss:

@import '~ngx-owl-carousel-o/lib/styles/scss/owl.carousel';
@import '~ngx-owl-carousel-o/lib/styles/scss/owl.theme.default';

Import RoutingModule and Routes into AppModule unless they are imported.

Import BrowserAnimationsModule into AppModule unless it is imported.

Import CarouselModule into a module which declares a component intended to have a carousel.

import { CarouselModule } from 'ngx-owl-carousel-o';
@NgModule({
  imports: [ CarouselModule ],
  declarations: [ CarouselHolderComponent ]
})
export class SomeModule { }

Add to needed component customOptions or named in different way object with options for the carousel:

import { OwlOptions } from 'ngx-owl-carousel-o';
@Component({
  selector: '....',
  templateUrl: 'carousel-holder.component.html'
})
export class CarouselHolderComponent {
  customOptions: OwlOptions = {
    loop: true,
    mouseDrag: false,
    touchDrag: false,
    pullDrag: false,
    dots: false,
    navSpeed: 700,
    navText: ['', ''],
    responsive: {
      0: {
        items: 1
      },
      400: {
        items: 2
      },
      740: {
        items: 3
      },
      940: {
        items: 4
      }
    },
    nav: true
  }
}
Add html-markup to the template of the component (in this case, add it to carousel-holder.component.html):

  <div>Some tags before</div>
    <owl-carousel-o [options]="customOptions">
    <ng-template carouselSlide>Slide 1</ng-template>  
    <ng-template carouselSlide>Slide 2</ng-template>  
    <ng-template carouselSlide>Slide 3</ng-template>  
  </owl-carousel-o>
  <div>Some tags after</div>

or

<div>Some tags before</div>
    <owl-carousel-o [options]="customOptions">

    <ng-container *ngFor="let slide of slidesStore">
      <ng-template carouselSlide [id]="slide.id">
        <img [src]="slide.src" [alt]="slide.alt" [title]="slide.title">
      </ng-template>
    </ng-container>

  </owl-carousel-o>
  <div>Some tags after</div>

please follow this link

Upvotes: 0

jitender
jitender

Reputation: 10429

You can use OWl Carousel for that something like

 <div class="owl-carousel owl-theme">
       <div class="item" *ngFor="let data of resource let i = index;">
 <mat-grid-list cols="2" rowHeight="30px" class="carousel-data"  (click)="showdata()" >

        <mat-grid-tile>     
           <img class="m-t-0 m-b-0 "[src]="data.img" alt="" width="24">
            <div class="prof-left" style="margin-left: 10px;">
                <p class="m-t-0 m-b-0 bank-name">{{ data.bank_name}}</p>
                <p class="m-t-0 m-b-0 bank-account">{{ data.account_number }}</p>
            </div>
        </mat-grid-tile>
        <mat-grid-tile>
            <div class="prof-right">
                <p class="m-t-0 m-b-0 total-bal">Total Bal</p>
                <p class="m-t-0 m-b-0 total-amount">{{ data.amount }}</p>
            </div>
        </mat-grid-tile>
    </mat-grid-list>
    </div>
    </div>

in your componenet

ngAfterViewInit(){
    $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true,
    responsive:{
        0:{
            items:1
        },
        600:{
            items:3
        },
        1000:{
            items:5
        }
    }
   })
  }

Check this demo to see how to use with angular

note you have to include jquery and owl carousel file to make it work

Or you can use ngx-owl-carousel

Upvotes: 0

Related Questions