Encountering an error in Angular where the property does not exist in type

Struggling to create a collapsible menu within my header component in an Angular project, I've hit a snag with proper JSON formatting. The error message that keeps popping up reads:

Error: src/app/components/header/header.component.html:48:49 - error TS2339: Property 'sublinks' does not exist on type '{ title: string; collapsed: boolean; links: { title: string; url: string; sublinks: { title: string; url: string; }[]; }[]; }'.

48 <li *ngFor="let sublink of item.sublinks" class="sublink">

I'm at a loss trying to troubleshoot this issue on my own and am in need of some assistance.

This is what my HTML template looks like:

<li *ngFor="let item of navigationItems; let i = index" class="link">
            <a [href]="item.url" class="main-wrapper">{{ item.title }}</a>
            <a class="d-inline-block main-wrapper" (click)="toggleCollapse(i)" role="button" [attr.aria-expanded]="!item.collapsed" [attr.aria-controls]="'navCollapse_' + i">
              <img *ngIf="item.collapsed" src="./assets/img/icons/plus.svg" alt="plus-icon" class="ms-3">
              <img *ngIf="!item.collapsed" src="./assets/img/icons/minus.svg" alt="minus-icon" class="ms-3">
            </a>
            <div [id]="'navCollapse_' + i" class="collapse link-collapse main-collapse" [class.show]="!item.collapsed">
              <ul class="sublink-wrapper">
                <li *ngFor="let sublink of item.sublinks" class="sublink">
                  <a [href]="sublink.url">{{ sublink.title }}</a>
                </li>
              </ul>
            </div>
          </li>

And here's the TypeScript code:

export class HeaderComponent {
  constructor() { }

  navigationState: string = 'out';
  
  navigationItems = [
    {
      title: 'Lorem ipsum',
      collapsed: true,
      links: [
        {
          title: 'Lorem ipsum 1',
          url: 'some-url',
          sublinks: [
            { title: 'Lorem ispum 1.1', url: 'some url' },
            { title: 'Lorem ispum 1.2', url: 'some url' },
            { title: 'Lorem ispum 1.3', url: 'some url' }
          ]
        },
        {
          title: 'Lorem ipsum 2',
          url: 'some-url',
          sublinks: [
            { title: 'Lorem ispum 2.1', url: 'some url' },
            { title: 'Lorem ispum 2.2', url: 'some url' },
            { title: 'Lorem ispum 2.3', url: 'some url' },
            { title: 'Lorem ispum 2.4', url: 'some url' }
          ]
        },
        {
          title: 'Lorem ipsum 3',
          url: 'some-url',
          sublinks: [
            { title: 'Lorem ispum 3.1', url: 'some url' },
            { title: 'Lorem ispum 3.2', url: 'some url' },
            { title: 'Lorem ispum 3.3', url: 'some url' }
          ]
        },
        {
          title: 'Lorem ipsum 4',
          url: 'some-url',
          sublinks: [
            { title: 'Lorem ispum 4.1', url: 'some url' },
            { title: 'Lorem ispum 4.2', url: 'some url' },
            { title: 'Lorem ispum 4.3', url: 'some url' },
            { title: 'Lorem ispum 4.4', url: 'some url' },
            { title: 'Lorem ispum 4.5', url: 'some url' }
          ]
        },
      ]
    }
  ];

  ngOnInit(): void {
  }

  toggleCollapse(index: number): void {
    this.navigationItems[index].collapsed = !this.navigationItems[index].collapsed;
  }

}

Answer №1

Jaromanda X has pointed out that the `sublinks` property belongs to an object within the `links` property. Therefore, it is necessary to first iterate over 'links'. In total, there should be 3 *ngFor directives, not just 2: the first one for `navigationItems`, then `links`, and finally `sublinks`. It is also suggested to consider replacing the `*ngIf` with a ternary operator for src and alt attributes. As a result, the code should look something like this:

<li *ngFor="let item of navigationItems; let i = index" class="link">
      <a class="main-wrapper" (click)="toggleCollapse(i)" role="button" [attr.aria-expanded]="!item.collapsed" [attr.aria-controls]="'navCollapse_' + i">
        {{ item.title }}
        <img [src]="item.collapsed ? './assets/img/icons/plus.svg' : './assets/img/icons/minus.svg'" [alt]="item.collapsed ? 'plus-icon' : 'minus-icon'" class="ms-3">
      </a>
      <div [id]="'navCollapse_' + i" class="collapse link-collapse main-collapse" [class.show]="!item.collapsed">
        <ul class="sublink-wrapper">
          <li *ngFor="let link of item.links" class="sublink">
            <a [href]="link.url">{{ link.title }}</a>
            <ul *ngIf="!item.collapsed" class="sub-sublink-wrapper">
              <li *ngFor="let sublink of link.sublinks" class="sub-sublink">
                <a [href]="sublink.url">{{ sublink.title }}</a>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </li>

Answer №2

constructor() { }

  navigationState: string = 'out';
  
  navigationItems:any = [
    {
      title: 'Unique Content',
      collapsed: true,
      links: [
        {
          title: 'Unique Content 1',
          url: 'some-url',
          sublinks: [
            { title: 'Unique Content 1.1', url: 'some url' },
            { title: 'Unique Content 1.2', url: 'some url' },
            { title: 'Unique Content 1.3', url: 'some url' }
          ]
        },
        {
          title: 'Unique Content 2',
          url: 'some-url',
          sublinks: [
            { title: 'Unique Content 2.1', url: 'some url' },
            { title: 'Unique Content 2.2', url: 'some url' },
            { title: 'Unique Content 2.3', url: 'some url' },
            { title: 'Unique Content 2.4', url: 'some url' }
          ]
        },
        {
          title: 'Unique Content 3',
          url: 'some-url',
          sublinks: [
            { title: 'Unique Content 3.1', url: 'some url' },
            { title: 'Unique Content 3.2', url: 'some url' },
            { title: 'Unique Content 3.3', url: 'some url' }
          ]
        },
        {
          title: 'Unique Content 4',
          url: 'some-url',
          sublinks: [
            { title: 'Unique Content 4.1', url: 'some url' },
            { title: 'Unique Content 4.2', url: 'some url' },
            { title: 'Unique Content 4.3', url: 'some url' },
            { title: 'Unique Content 4.4', url: 'some url' },
            { title: 'Unique Content 4.5', url: 'some url' }
          ]
        },
      ]
    }
  ];

  ngOnInit(): void {
  }

  toggleCollapse(index: number): void {
    this.navigationItems[index].collapsed = !this.navigationItems[index].collapsed;
  }

}
<li *ngFor="let item of navigationItems; let i = index" class="link">
    <a [href]="item?.url" class="main-wrapper">{{ item.title }}</a>
    <a class="d-inline-block main-wrapper" (click)="toggleCollapse(i)" role="button" [attr.aria-expanded]="!item.collapsed" [attr.aria-controls]="'navCollapse_' + i">
      <img *ngIf="item.collapsed" src="./assets/img/icons/plus.svg" alt="plus-icon" class="ms-3">
      <img *ngIf="!item.collapsed" src="./assets/img/icons/minus.svg" alt="minus-icon" class="ms-3">
    </a>
    <div [id]="'navCollapse_' + i" class="collapse link-collapse main-collapse" [class.show]="!item.collapsed">
      <ul class="sublink-wrapper">
        <li *ngFor="let sublink of item?.sublinks" class="sublink">
          <a [href]="sublink.url">{{ sublink.title }}</a>
        </li>
      </ul>
    </div>
  </li>

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

Tips for integrating Chart.js into my AngularJS application?

I am a beginner with AngularJS and I'm currently developing an application on Ubuntu. While trying to add Chart.js using npm install chart.js, an error is being displayed as follows. npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_emai ...

Having trouble with JSON syntax in your PHP function?

Having trouble parsing JSon Data in my Android application. I'm using a PHP script to retrieve data from MySQL and convert it into JSON format. Although I can successfully print the result and view all items from the 'people' table, when the ...

Is it possible to incorporate the arrow function within the debounce function?

export const debounce = (callback: Function, ms = 300) => { let timeoutId: ReturnType<typeof setTimeout> return function (...args: any[]) { clearTimeout(timeoutId) timeoutId = setTimeout(() => callback.apply(this, args), ms) ...

Having Trouble with Shopify's Asynchronous Ajax Add to Cart Feature?

I have developed a unique Shopify page design that allows users to add multiple items to their cart using Shopify's Ajax API. I've created a test version of the page for demonstration: Below is the current javascript code I am using to enable as ...

What is the best method for interacting with multiple links in Puppeteer?

As a beginner with puppeteer, I am diving into the world of web scraping by attempting to create a simple scraping task. My Plan of Action The plan is straightforward: Visit a page, Extract all <li> links under a <ul> tag, Click on each < ...

tips for extracting data from a json array

Hello everyone, I have a question that I could use some help with. I have a set of data that looks like this: var data = { "values":[[1,2,3],[2,4,3],[3,6,7],[1,4],[6,4,3,4],[6,7,3,5]] } Currently, I am trying to create a multiple line chart usi ...

Alter the background of cards in real-time

I am unfamiliar with JavaScript, but I wish to utilize a function to alter the background color of multiple cards. function ChangeColorToGreen() { document.getElementById("ChangingCard").style.background = "green"; } function ChangeColorToPurple() { ...

Parsing JSON data received from Hibernate

I encountered an issue with deserializing a class in my Spring Boot application. Whenever the controller attempts to deserialize it, the application crashes. Below is the structure of the class causing the problem: @Entity @Table(name="trash_cans") @JsonI ...

Is it possible to transfer elements from one array to another when clicked, but without copying the contents to the new array objects?

Welcome, For my latest project, I am excited to create a "Learning Cards" App from scratch. The concept is pretty straightforward: it consists of cards with questions. Upon clicking a button, you can reveal the correct answer. Additionally, there's a ...

Finding the Attachment ID on a JIRA Issue Page with JavaScript

Currently, I am using an ajax call that requires the attachment id in its URL. The URL is hardcoded as follows: url: AJS.contextPath()+"/rest/api/latest/attachment/10415" jQuery.ajax({ url: AJS.contextPath()+"/rest/api/latest/attachment/10415", TYPE: "GET ...

Utilizing Vuex to manage the state of modal components within a list of child components

I am attempting to utilize vuex to maintain a value that controls the visibility of a modal in a child component. The parent component renders the child component multiple times in a list, but the modal consistently displays only the last item in the list. ...

Controlling dropdown menus filled with AJAX responseData

When it comes to Javascript, JQuery has always been a reliable companion for me. However, there are instances where I encounter challenges that require some extra effort to overcome. Today happens to be one of those days. I've stumbled upon an issue t ...

What are some strategies for enhancing TypeScript/Node speed in VSCode with the help of WSL2 and Docker?

My development environment consists of VSCode running on Windows (v1.58.2) with the Remote WSL extension (v0.58.2). I have Docker Desktop (3.5.2, engine: 20.10.7) set up to work with Linux containers through the WSL2 backend. Within these containers, I a ...

Utilize D3 for showcasing SVG graphics inside an Angular Element

I'm having trouble incorporating a simple SVG image into my Angular component using D3. I feel like I might be missing something obvious and just not seeing it. In my package.json file, D3 is listed as: "d3": "5.1.0". I've also created an Angula ...

Angular 2- Unable to bind to 'ngSwitchCase' as it is not recognized as a native property

I am facing an issue with my code where I have two lists that are displayed in my .html file. In order to avoid repetition, I decided to utilize ngSwitch. However, when I try to implement this approach, I encounter an error. Here is the snippet of code cau ...

How to decode and access a nested JSON array in Swift: A guide

I'm struggling with parsing 10 arrays within an array from JSON to Swift and accessing the data. While I can successfully fetch the data via a REST API and print it out on the console, I am unsure of how to save it for further processing. JSON array ...

What is the best way to retrieve information from a webpage I have created using an express backend and an HTML frontend

I designed a login page named index.html that prompts users to enter their email and password, followed by a submit button with the label "Log In": <input type="submit" value="Log In" id="loginbutton"> Within my app.js ...

What is the key to ensuring the content in your canvas adapts to different screen sizes

Greetings! I wanted to extract the values from this specific meta tag: <meta name="viewport" property="viewport" content="width-device-width, initial-scale=1"> To retrieve content from a meta tag using JavaScript, I used the following code snippet: ...

Tips for successfully executing child_process.exec within an ajax request

On a server that I have access to but not ownership of, there is a node js / express application running on port 3000. Various scripts are typically executed manually from the terminal or via cron job. My goal is to have a button on the client-side that tr ...

Transfer results of SQL query from PHP to JavaScript array

Every day, I manually update a JavaScript variable in the following way: <script> var data = [ ['Austria','IBM','8284927','UF93NV', '10'], ['Spain','Dell&ap ...