What is the best way to display data from an array using ng-container in Angular 2?

My goal is to display data from an array in an HTML table using the Section model provided below:

export class Section {
    public id :number;
    public name: string;
    constructor(id: number, theName: string) {
        this.id = id;
        this.name = theName;
    }
}

First, I import the Section model into my component:

import { Section } from "../models/registerSection.model";

Next, I declare an array:

sectionList: Array<Section>;

The array is populated in the constructor like so:

    this.sectionList = [new Section(1, 'A'),
        new Section(2, 'B*'),
        new Section(3, 'C'),
        new Section(4, 'D')];

Here's how I attempt to render the data in the template:

        <ng-container *ngFor='let data of sectionList'>
            <tr>
                <td colspan="7">{{data.name}}</td>
            </tr>
        </ng-container>

However, the table remains empty and the DOM shows:

<!--template bindings={
  "ng-reflect-ng-for-of": null
}-->

I'm puzzled by this issue since I've confirmed that the array does contain the necessary data during debugging. What could be causing this problem?

Answer №1

Here is the correct way to iterate through the sectionList:

<ng-container *ngFor='let data of sectionList'>

You should be iterating through the sectionList instead of the Section model directly.

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

Transferring Variables from WordPress PHP to JavaScript

I have implemented two WordPress plugins - Snippets for PHP code insertion and Scripts n Styles for JavaScript. My objective is to automatically populate a form with the email address of a logged-in user. Here is the PHP snippet used in Snippets: <?p ...

The CORS policy has prevented access to 'https://localhost:7144/api/employees' from 'http://localhost:3000'

I encountered an error while attempting to retrieve data from a web API to exhibit in a React JS application. The console displayed this image of the error Below is a snippet from my program.cs file: (Code snippet from program.cs goes here) Additionally ...

Encountering a memory issue while trying to read and display Excel data in an Angular 2 application

Currently, I am working on an asp.net web API that utilizes OpenXML SDK for Excel to read data from an Excel file and display it in an Angular2 application. The dataset consists of approximately 70,000 rows that need to be displayed all at once without any ...

Steps for handling multiple arrays in a client-side ajax request

I've encountered an issue while trying to process 5 arrays in an AJAX call - only two are being decoded. Despite spending a considerable amount of time debugging for one and half days, I am unable to pinpoint the root cause of this problem. The AJAX c ...

What are some strategies for efficiently displaying a large amount of data on a screen using Javascript and HTML without sacrificing performance?

Imagine having a hefty 520 page book with over 6,200 lines of text ranging from 5 to 300 characters each. The challenge lies in efficiently displaying this content on the screen for users to read without causing lag or performance issues. Attempting to re ...

Error: The property 'template' is not defined and cannot be read

https://i.sstatic.net/G5QPW.png Data table not loading properly ** JavaScript code ** displayedColumns = ['bloodpressure', 'username', 'weight', 'height']; @ViewChild(MatPaginator) paginator: MatPaginator; ...

When utilizing the catch function callback in Angular 2 with RxJs, the binding to 'this' can trigger the HTTP request to loop repeatedly

I have developed a method to handle errors resulting from http requests. Here is an example of how it functions: public handleError(err: any, caught: Observable<any>): Observable<any> { //irrelevant code omitted this.logger.debug(err);//e ...

Vue.js Dynamic Class Binding Fails to Update

As I delve into learning Vue Js, I have been working on a simple todo app. One interesting thing I've done is binding a class to my todo items based on whether todo.completed is true or not: <a v-bind:class="{iscomplete : todo.completed}& ...

There was an error in Threejs' PropertyBinding as it attempted to parse the trackName ".bones[].position

Version: THREE.WebGLRenderer 91dev Struggling to achieve a basic chest opening animation using three.js. Unfortunately, encountering an error while trying to create an action. PropertyBinding: Unable to interpret trackName: .bones[].position Link to t ...

Generating prime numbers in Javascript

My attempt at generating the prime numbers less than 20 using my current knowledge is as follows: let arr = []; for (let x = 3; x <= 20; x++) { for (let i = 20; i > 0; i--) { if (x % i !== i) { arr.push(x) } } console.log(arr) ...

Having trouble converting the JSON object received from the server into the necessary type to analyze the data

I am new to angular and struggling with converting a JSON object from the server into a custom datatype to render in HTML using ngFor. Any help would be appreciated as I have tried multiple approaches without success. Apologies for the simple HTML page, I ...

Angular front-end rendering with Rails backend integration

Currently, I am working on a medium-sized Rails application and my latest endeavor is to integrate Angular into the system. After reviewing several tutorials, it appears that the most common method for fetching initial data and displaying the first view in ...

Converting MiniZinc CSP to JSON using a workaround for iterating through arrays in JavaScript

Utilizing the node.js module "Governify CSP Tools" to tackle a CSP issue has been challenging. Despite following the guidelines on defining an array from the CSP model schema (https://www.npmjs.com/package/governify-csp-tools), I have encountered syntax er ...

Identify a variety of potential types within an array passed using the spread operator

I'm on a quest to deduce the type for every spread argument of my type function. Suppose I have two fields defined as follows. type Field<T> = { value: T, name: string } const field1 = { value: 12, name: 'age' } const field2 = { valu ...

The setTimeout() function caught in an endless cycle

Is there a way to retrieve the height of divs used in multiple components? The HTML structure I am working with is as follows: <div #dataHeadlines *ngFor="let group of catt" [ngClass]='hf(dataHeadlines)'> <h4>{{ group }}< ...

Tips for building a carousel-style transition using React Router

I am looking to implement a carousel animation in my React Router. My website has pages named A, B, C, and D. When transitioning from page A to B, I want the animation to move from right to left. When going from B to A, I want it to move from left to rig ...

Javascript on-page scroll positioning

My current dilemma involves finding a solution to optimize in-page scrolling for mobile users. I have a sticky header on the page, which causes #section1 to place the scroll position behind the sticky header. Is there a way to adjust the scroll position b ...

"Utilizing the mongodb deleteOne function to remove an email within a document

In my collection, I have the following document model: { "user":{ "name":string, "email":string }, "address":{ "direction":string, "country":string }, "id":string } If you want to delete the document by id, there&apo ...

Guide to starting a typed array in Typescript

I have an array called "cart" of objects that is initially empty, but structured like this: cart:[{name:any, rate:number, qty:number, discount:number, subtotal:number}] When I attempt to add data to it like so: cart:[{name:any, rate:number, qty:number, ...

Nesting / Mulled / JS - Uploading Files - Form's end is unexpectedly reached

I have successfully implemented the upload file functionality in my Nest.js server application, but I am facing an issue when trying to use it with JavaScript/React. @Post('upload') @UseInterceptors(FileInterceptor('file')) upl ...