Exploring Ngu-Carousel in Angular 7: Importing JSON data for a dynamic display

After attempting to import data from JSON and display it using ngu-carousel, I encountered an error.

The error shows "length of undefined"

Subsequently, when I try to click on the previous/next button, another error appears.

This error states "innerHTML of undefined"

Below is a snippet of my code:

gallery.component.ts

import { Component, OnInit } from "@angular/core";
import { NguCarouselConfig } from "@ngu/carousel";
import { ShopService } from "../shop.service";

@Component ({ selector: "app-gallery", templateUrl: "./gallery.component.html" })

export class GalleryComponent implements OnInit {

public product: any;

public carouselImg: NguCarouselConfig = {
  grid: { xs: 1, sm: 2, md: 3, lg: 3, all: 0 },
  slide: 1,
  loop: true,
  speed: 250,
  point: {
   visible: true,
  },
  load: 2,
  velocity: 0,
  touch: true,
  };

constructor (private sv: ShopService) {};

ngOnInit () {
 this.sv.getProducts().subscribe(data => {
   this.product = data;
  });
 };
}

gallery.component.html

    <ngu-carousel #imgCarousel [inputs]="carouselImg" [dataSource]="product">
      <ngu-tile *nguCarouselDef="let item">
        <mat-card>
            <mat-card-header>
                <mat-card-title>{{item.name}}</mat-card-title>
            </mat-card-header>
            <img mat-card-image [src]="item.photo" alt="">
            <mat-card-content>
                <p>
                    {{item.remark}} <span><a href="">More info</a></span>
                </p>
            </mat-card-content>
        </mat-card>
      </ngu-tile>
      <button NguCarouselPrev class="leftRs">&lt;</button>
      <button NguCarouselNext class="rightRs">&gt;</button>
    </ngu-carousel>

I am unsure of what is causing the issue in my code. Any assistance would be greatly appreciated :)

Answer №1

After reviewing both versions, I suggest incorporating the following code into your component:

@ViewChild('imageSlider') imageSlider: NguCarousel<any>;

It is important to include a default value to prevent any issues from arising initially:

public items = [];

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

What is the best way to generate an array from JSON data while ensuring that the values are not duplicated?

Upon receiving a JSON response from an API, the structure appears as follows: { "status": "success", "response": [ { "id": 1, "name": "SEA BUSES", "image": null }, { "id": 2, ...

What is the best way to host a single page application within a sub-directory using nginx?

Trying to set up nginx to host an Angular application from a unique child path. Adjusted the app to use a base href of fish, able to serve root page and assets correctly. However, encountering a 404 error when attempting to reload the page on a child rout ...

`In NestJS Nested Schema, the @Prop decorator and mongoose options are not applied as expected

I'm currently working on constructing a Schema that includes a nested object. I am trying to define default values and required properties within the nested object, but it seems like the options I set are being ignored. task.entity.ts @Schema() expor ...

Incorporating sass into your Antd and Create React App workflow

Trying to combine SASS with antd and CRA has been a bit of a challenge for me. Despite following various tutorials, most of them seem outdated and result in errors. Fortunately, I stumbled upon an article that actually works smoothly link However, I can& ...

Implementing Conditional Display of Span Tags in React Based on Timer Duration

In my current React project, I am facing an issue with displaying a span tag based on a boolean value. I need assistance in figuring out how to pass a value that can switch between true and false to show or hide the span tag. I tried two different methods ...

Importing a 3D Model Using Three.js

I've been trying to import a 3D model by following tutorials. I managed to successfully import using A-Frame Tags, but encountering issues with Three.js. The code snippets are from a tutorial on YouTube that I referred to: https://www.youtube.com/watc ...

The behavior of the 'typeof null' function in JavaScript is not functioning

I have a collection of objects where each object contains a key and an array as a value. You can see what I mean in this image. Some of the arrays had less than 20 elements, so I wrote some code to pad them with zeros. The result of running my code can be ...

Arrange the angular datatables in a specific order without displaying the usual user interaction triangles

I am looking to arrange the data in a fixed manner without any user interaction for sorting. However, it seems that I can either completely disable ordering like this: this.dtOptions = { paging: false, lengthChange: false, searching: false, orderi ...

Tips for transferring data from a pop-up or modal window to the main window in ASP.NET MVC

Currently, I am in the process of developing a contact form within ASP.NET MVC. This contact form will allow users to easily attach regular files through traditional file and browse functions. Additionally, users will have the option to search for a specif ...

Display a confirmation dialog using AngularConfirm after a function in AngularJS has finished executing

Trying to figure out how to update the $ngConfirm box after a function is done. When submit is clicked, a spinning cog appears: $scope.inProgress = function(){ $ngConfirm({ theme: 'modern', icon: "fa fa-cog fa-spin fa-.5x ...

Vue component's data remains stagnant within created() hook

I'm currently working on transforming the API response to make it more suitable for constructing two tables. Despite adding debugging outputs within my function in created(), I am witnessing the desired output temporarily, but upon further examination ...

React-native horizontal sliding plugin

Can anyone recommend a reliable horizontal slider plugin for react-native? I've come across some options, but they haven't been working as smoothly as I'd hoped. ...

I'm having trouble getting my home.js to render in the outlet component

After using Material-UI to create navbar.js, I encountered an issue where the other component was not rendering in the <Outlet/> Component RootLayout.js import { Outlet } from "react-router-dom"; import NavBar from "../component/NavBa ...

Difficulty in converting class-based JS useState array to TypeScript Hook

Recently, I've delved into the world of React / TypeScript and have taken on a personal project to enhance my skills. As part of this project, I am in the process of transitioning some class-based code into hooks. Here is the (JS) class-based code th ...

Having trouble removing the angular-cli from your system

I've attempted multiple times to remove my angular-cli for the purpose of updating it, but despite following the instructions provided on GitHub: npm uninstall -g @angular/cli npm cache clean npm install -g @angular/cli@latest Even after running th ...

Customize the appearance of radio buttons in HTML by removing the bullets

Is there a way for a specific form component to function as radio buttons, with only one option selectable at a time, without displaying the actual radio bullets? I am looking for alternative presentation methods like highlighting the selected option or ...

What is the reason for the Client Height value not affecting other elements?

I'm trying to set the spacing of my sidebar from the top equal to the height of the header. However, when I use clientHeight in JavaScript to get the height and then try to apply it to other elements using marginTop or top values (with position includ ...

What is the best method for checking for JavaScript errors when using Selenium WebDriver in conjunction with NodeJS?

When it comes to coding in JavaScript, not Java, my focus is on running a specific website like foo.com. I typically set it up as follows: var webdriver = require("selenium-webdriver"); By = webdriver.By, until = webdriver.until; var chrome = req ...

An HTML button generated by a .js file is able to execute a .js function without any issues, but it inadvertently removes all .css styling

Currently grappling with an issue in my JavaScript self-teaching journey that I can't seem to resolve or find examples for. The problem at hand: When the HTML button calls a .js function, it successfully executes the function but also wipes out all C ...

What could be the reason for PassportJS in Node failing to clear the session upon logout?

I am encountering an issue with my system not successfully logging out using PassportJS. The logout route appears to be triggered, but the session is not being removed as intended. I would like it to return a 401 error if the user is not logged in on a spe ...