Different ways to incorporate the div element with text content as opposed to images in ngx-slick-carousel

I am looking to incorporate multiple <div> elements with content into the ngx-slick-carousel in my Angular 12 application. Currently, the carousel is displaying placeholder images and I am unsure of how to replace these images with div elements.

Here is the code snippet I am currently using:

app.component.html

<ngx-slick-carousel class="carousel" #slickModal="slick-carousel"
                    [config]="slideConfig" 
                    (init)="slickInit($event)"
                    (breakpoint)="breakpoint($event)"
                    (afterChange)="afterChange($event)"
                    (beforeChange)="beforeChange($event)">
     
     <div ngxSlickItem *ngFor="let slide of slides" class="slide"><img src="{{ slide.img }}" alt="" width="100%"></div>

</ngx-slick-carousel>

app.component.ts

  slides = [
    {img: "https://via.placeholder.com/600.png/021/fff"},
    {img: "https://via.placeholder.com/600.png/321/fff"},
    {img: "https://via.placeholder.com/600.png/422/fff"},
    {img: "https://via.placeholder.com/600.png/654/fff"}
  ];
  slideConfig = {"slidesToShow": 2, "slidesToScroll": 1};

I would appreciate any guidance on how I can implement div elements instead of image URLs for the carousel. Thank you.

Answer №1

You have the flexibility to include multiple div elements within the carousel instead of just images.

For instance, you can insert two separate div elements and display only one at a time.

To achieve this, update your slideConfig object in your TypeScript file as shown below.

  slideConfig = {
              "slidesToShow": 1, 
              "slidesToScroll": 1, 
              "arrows": false
            };

Next, within the <ngx-slick-carousel> tag in your HTML file, add two individual ngxSlickItem components without using *ngFor.

 <ngx-slick-carousel 
              class="carousel" 
              #slickModal="slick-carousel" 
              [config]="slideConfig">

      <div ngxSlickItem class="slide">
        <h1>First slide</h1>
      </div>

      <div ngxSlickItem class="slide">
        <h1>Second slide</h1>
      </div>

 </ngx-slick-carousel>

Now, you can easily navigate using the previous and next buttons.

<button (click)="slickModal.slickPrev()">Previous</button>
<button (click)="slickModal.slickPause()">Pause</button>

If you want to add more than two div elements, simply insert additional ngxSlickItem components with divs and adjust the value of slidesToShow in slideConfig according to your needs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to determine if a route, or any of its nested routes, are currently active

Consider the following route examples: <Routes> <Route path="admin"> <Route path="users"> <Route index element={<UserList />} /> <Route path="create" element={<UserDetails ...

Oops! The API request was denied with error code 401 - Unauthorized in React

I have been working on incorporating an API into my front-end project using React/Typescript. The documentation for the API specifies that authorization requires a key named token with a corresponding value, which should be included in the header. To stor ...

Challenges with formArrayName in Angular can be tricky to navigate

I am in the process of developing an Angular library with the main feature being the provision of a selector with two inputs: a reactive form containing the data an object literal specifying which properties of the data should have editable input fields ...

Problem with the placement of Google autocomplete dropdown

I'm currently utilizing the NgxAutocomPlace module within my Angular application. This module interacts with the Google Autocomplete API by generating a .pac-container to display autocomplete suggestions. However, I'm facing an issue where on mo ...

What is the best way to provide JSON data instead of HTML in Angular?

Is it possible to output processed data as json instead of html? I want to process backend data and output it as json for a specific url. How can I prepare a component to do this? Currently, the app serves html pages where components process backend data ...

angular reactive form: communicate between child and parent components

Having trouble passing values from a child component to its parent in Angular's reactive forms. Any advice? Here is an example of the parent form: <div class="fields-wrap" [formGroup]="parent"> <div class="input-wrap"> <child-compon ...

Determining if a component is nested within itself in Angular 6 is a crucial task

My goal is to develop a custom Angular component for a nested navigation menu with multiple levels. Below is an example of how the menu structure looks: app.component.html <nav-menu> <nav-menu-item>Section 1</nav-menu-item> <nav- ...

Use a mock HTTP response instead of making an actual server call in Angular 4

I am facing a scenario where I have a function myFunc() that I subscribe to. When this function is called with X parameter, I expect a regular HTTP response from the server. However, if it is called without the X parameter, I want it to return a 'mo ...

shifting the angular directives to alternate the bootstrap class of hyperlinks

I have a collection of hyperlinks displayed on my webpage. <a href="#" class="list-group-item list-group-item-action active" routerLink='/route1' >Explore First Link</a> <a href="#" class="list-group-item list-group-item-action" r ...

The h2 class does not appear to be taking effect when loading dynamically

Looking to dynamically insert an h2 element with a specific class into a div. <div class="row gx-4" [innerHTML]="holidaydeals"> </div> My current attempt involves dynamically adding an h2 heading to the div above. ...

Retrieve functions with varying signatures from another function with precise typing requirements

My code includes a dispatch function that takes a parameter and then returns a different function based on the parameter value. Here is a simplified version: type Choice = 'start' | 'end'; function dispatch(choice: Choice) { switch ...

What is the reason behind the error Generic indexed type in Typescript?

Here is a scenario where I have a specific generic type: type MapToFunctions<T> = { [K in keyof T]?: (x: T[K]) => void; }; It functions correctly in this instance: type T1 = { a: string }; const fnmap1: MapToFunctions<T1> = { a: (x: st ...

Why is it that the HttpClient constructor in Angular doesn't require parameters when instantiated through the constructor of another class, but does when instantiated via the 'new' keyword?

I am trying to create a static method for instantiating an object of a class, but I have encountered a problem. import { HttpClient } from '@angular/common/http'; export MyClass { // Case 1 public static init(): MyClass { return this(new ...

Is it possible to implement a redirect in Angular's Resolve Navigation Guard when an error is encountered from a resolved promise?

I have integrated Angularfire into my Angular project and am utilizing the authentication feature. Everything is functioning properly, however, my Resolve Navigation Guard is preventing the activation of the component in case of an error during the resolve ...

Is Axios the sole option for API calls when utilizing Next.js with SSG and SSR?

Can someone clarify the best practice for data fetching in Next.js? Should we avoid using axios or other methods in our functional components, and instead rely on SSG/SSR functions? I'm new to Next.js and seeking guidance. ...

Compilation of TypeScript in MSBuild is failing during the Docker build process, while it is successful in the Azure pipeline

I am currently working on containerizing an asp.net .net framework web application. Most of it is functioning correctly. However, I have encountered an issue where my .ts files are not being compiled for some reason. The build process runs smoothly in an ...

How should dynamic route pages be properly managed in NextJS?

Working on my first project using NextJS, I'm curious about the proper approach to managing dynamic routing. I've set up a http://localhost:3000/trips route that shows a page with a list of cards representing different "trips": https://i.stack. ...

ngIf not working properly with the updated value of [(ngModel)]

I am encountering an issue with a select element that has options. The select is using the [(ngModel)] directive to save selected values into "rightFieldTypeId." I have elements that should be displayed based on the value of "rightFieldTypeId." Even though ...

Guide to setting data types for [key, value] pairs within a forEach iteration

I am currently encountering a typescript syntax error in my project that is indicating the need to define the prodStatus variable... const products = { 1: {isUpdating: false, qty: 2}, 2: {isUpdating: true, qty: 4} } const updatingProducts: Array< ...

Error encountered while attempting to build Ionic 5 using the --prod flag: The property 'translate' does not exist on the specified type

I encountered an error while building my Ionic 5 project with the --prod flag. The error message I received is as follows: Error: src/app/pages/edit-profile/edit-profile.page.html(8,142): Property 'translate' does not exist on type 'EditProf ...