What is the best way to attach a statically typed array in an HTML document?

I am new to Angular 2 and TypeScript.

Currently, I have a Schoolyears component with the following code:

export class SchoolyearsComponent implements OnInit {

    schoolyears: Schoolyear[] = new Array();
    constructor(
        private _router: Router,
        private _schoolyearsService: SchoolyearsService) {
    }

    ngOnInit()
    {
        this._schoolyearsService.getSchoolyears().subscribe(s => {
            this.schoolyears.push(new Schoolyear(s));
        });
    }
}

The data that needs to be displayed in the UI is json data. To achieve this, I must encapsulate the json data within a custom Schoolyear.ts class before binding it to the UI.

export class Schoolyear {

    constructor(obj)
    {
        this.id = obj.id;
        this.name = obj.name;
        this.startDate = new Date(obj.startDate);
        this.endDate = new Date(obj.endDate);
    }

    id: number;
    name: string;
    startDate: Date;
    endDate: Date;
}

<div>
    <div *ngFor="#s of schoolyears">
        <div style="font-weight:bold;">
            <h4>{{s.id}}</h4>
            <h4>{{s.name}}</h4>
            <p>{{ s.startDate}}</p>
            <p>{{ s.endDate}}</p>
        </div>
    </div>
 </div>

Despite correctly implementing the properties, none of them are showing up on the UI.

What do I need to change in order to wrap the json data properly and showcase it in the UI?

There are no errors being displayed in the console output.

Answer №1

Here's an approach I would consider:

this._schoolyearsService.getSchoolyears().map(years => {
  return years.map(year => new Schoolyear(year));
}).subscribe(years => {
  this.schoolyears = years;
});

When applying this in your scenario, you are passing the entire list as a parameter to the Schoolyear class.

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

A guide on efficiently mapping all items from an object containing an unspecified number of items or item names within a reusable component

I am currently exploring the concept of building reusable components. In a personal project, I am in the process of mapping items retrieved from a database. My goal is to create a component that can dynamically map any number of unspecified types as table ...

Tips for retrieving selected values from local storage

I am working with a select component as shown below: <label> {{ "HOME.SELECT" | translate }} <select #langSelect (change)="translate.use(langSelect.value)" (change)='onOptionsSelected($event)'> <option *ngFor= ...

"Production environment encounters issues with react helper imports, whereas development environment has no trouble with

I have a JavaScript file named "globalHelper.js" which looks like this: exports.myMethod = (data) => { // method implementation here } exports.myOtherMethod = () => { ... } and so forth... When I want to use my Helper in other files, I import it ...

When incorporating a new group in a three.js Scene, the older object groups are automatically eliminated

Currently, I am developing a visual components designer that includes a functionality to group objects together under a specific name. When any of the grouped objects is selected, a red box should be displayed around them. However, I am facing an issue wh ...

Angular 7 - Merging Objects and Arrays

I have two models, Userc (with fields name, phone, email, etc.) and Usercco (same as Userc with an additional field coname for company name). In Userc model, I have coid field representing the company id. In my database (Firebase), I only have Userc and C ...

Running a Playwright test without relying on the command line

Is it possible to automate running a playwright test without having to manually input npx playwright test in the command line every time? I am looking for a way to initiate a playwright file from another file and have it execute without the need for acce ...

Experiencing Compatibility Issues: Next.js 14.0.3 with next-auth and NestJS Backend Runs Smoothly in Development Environment, but Encounters

Currently, I am developing a Next.js 14.0.3 application that utilizes next-auth for authentication. The application interacts with an external NestJS backend for authorization and JWT validation. While everything functions correctly in the development envi ...

Tips for sorting server components using a URL search parameter [Next.js, Prisma]

My goal is straightforward: I want to be able to filter my Prisma database based on parameters passed in the URL when I click on a CategoryBox. After clicking on a CategoryBox, my URL does change to something like http://localhost:3000/?category=XYZ, but ...

Is there a way to receive autocomplete suggestions for sequelize classes that extend the Model class?

I have a specific Post class that I've created with various properties defined within it. However, I often find myself struggling to remember all the fields when actually typing them out, leading to errors in my code. @Table class Post extends Model { ...

When conducting a test, it was found that the JSX element labeled as 'AddIcon' does not possess any construct or call signatures

Here is a code snippet I'm currently working with: const tableIcons: Icons = { Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />), Check: forwardRef((props, ref) => <Check {...props} ref={ref} />) }; const AddIcon ...

Adding an icon to the contents of a specific column in Angular material

Struggling to figure out how to incorporate an icon into the data in the Status column using Angular material. Here is the markup of my page: <table mat-table [dataSource]="dataSource"> <ng-container *ngFor="let ...

Winston's createLogger function is not defined

After setting up Jest unit tests in a TypeScript Node project, I encountered an issue where the main code broke after installing Jest with ts-node. Whenever I try to run npm test or npm start, I receive the following error: [nodemon] watching extensions: t ...

How to resolve useState problem in useEffect when state is not null

Encountering issues with maintaining state in TypeScript React. A Child component passes a 'terminal' object to the Parent via a function called returnTerminal(). This 'terminal' object is then stored as a useState variable named _obje ...

What is the process for transferring data processed in Python to Node.js and then forwarding it to Angular?

I am a newcomer to Angular and I'm looking for a way to showcase JSON data from Python in my Angular app using Node.js. I have already utilized child processes to establish the connection between Python and Node.js, but I am facing a challenge on how ...

Tips for troubleshooting a Keycloak-enabled Angular/Java/Spring application:

Recently, I developed a basic application that leverages keycloak for managing user authentication. However, I am interested in making certain routes accessible to the public. For example, something along the lines of localhost:4200/public-route. I attemp ...

Update your AngularJS to the most recent version of Angular

We are faced with the task of upgrading a legacy MVC website that currently utilizes AngularJs to the latest version of Angular. Surprisingly, our current website only scratches the surface of what Angular has to offer - mainly using it for databinding and ...

mat-tab-group - Positions elements in the center, right, and left for optimal alignment

Is it possible to align the buttons in a mat-tab-group to the left, center, and right positions? I am using mat-tabs. How can I have elements with "left" align to the left, elements with "center" in the center, and elements with "right" align to the right? ...

Retrieving the chosen option from ion-radio-group element in Ionic 4

Here is the code snippet I'm working with: <ion-item> <ion-radio-group [value]="profileItem.Sex" [(ngModel)]="profileItem.Sex"> <ion-label class="input-label">{{ 'GENDER' | translate }}</ion-label> <div style ...

Combine entities in Firebase when the names match

Currently, I am developing a simple shopping cart application. Whenever a user clicks on the Add To Cart button, I save the product in the database as an array item. If a user tries to add multiple items of the same product, I aim to group all products, ...

Encountering compilation errors with TypeScript files in Angular 2 while using Visual Studio 2017

Currently, I am in the process of developing an Angular 2 application Here is a snippet of the TypeScript code that I have written: import { Component } from 'angular2/core'; @Component({ selector: 'my-app', template: ' &l ...