*ngFor failing to refresh data after receiving server response

After utilizing Knockout for the past 5 years, I have decided to dive into the latest version of Angular for a change. Therefore, I am relatively new to Angular.

Problem

Upon clicking, a server call is executed which performs certain actions and returns a response. Everything seems to be functioning correctly on the server side, with the object being bound properly according to my debugging efforts. However, the UI refuses to update despite my attempts to configure the change tracking.

Code

Written in Typescript/Angular Markup

import { Component, Inject, ChangeDetectorRef } from "@angular/core";
import { HttpClient } from "@angular/common/http";

// Models
import { ContactRequest } from "../../Models/Contact/ContactDetailsRequest";
import { ContactDetailsResponse } from "../../Models/Contact/ContactDetailsResponse";

// Payloads
import { ContactBreakdownPayload } from "../../Payload/Contact/ContactPostPayload";

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

export class ContactComponent {
  private BaseUrl: string;
  private HttpClientObject: HttpClient;

  public ChangeDetection: ChangeDetectorRef;
  public Contacts: Array<ContactDetailsResponse> = [];

  constructor(httpClient: HttpClient, @Inject('BASE_URL') baseUrl: string, changeDetection: ChangeDetectorRef) {
    this.BaseUrl = baseUrl;
    this.HttpClientObject = httpClient;
    this.ChangeDetection = changeDetection;
  }

  public RetrieveContactDetails(): void {
    const payload = {
      ContactBreakdowns: ContactBreakdownPayload
    } as ContactRequest;

    this.HttpClientObject.post(`${this.BaseUrl}contact`, payload)
      .subscribe((response: Array<ContactDetailsResponse>) => {
        console.log(response);

        this.Contacts = response;

        console.log(this.Contacts);

        this.ChangeDetection.detectChanges();
      });
  }
}

HTML Markup

<button type="button"
        class="btn btn-outline-primary btn-block"
        (click)="RetrieveContactDetails()">
  Retrieve contacts
</button>

<table class="table table-striped"
       style="margin-top: 0.625rem">
  <thead class="thead-dark">
    <tr>
      <th scope="col">Title</th>
      <th scope="col">First name</th>
      <th scope="col">Last name</th>
      <th scope="col">Date of birth</th>
      <th scope="col">Age</th>
    </tr>
  </thead>

  <tbody>
    <tr *ngFor="let Contact of ContactDetails">
      <td>TEST</td>
      <td>TEST</td>
      <td>TEST</td>
      <td>TEST</td>
      <td>TEST</td>
    </tr>
  </tbody>
</table>

Question

From the provided information, does anyone spot where I might be making an error? It could simply be a basic implementation detail that I have overlooked.

Answer №1

Make sure to update the variable name in your code. You are currently saving server response in a variable named Contacts, so be sure to change *ngFor="let Contact of ContactDetails" to *ngFor="let Contact of Contacts"

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

Having trouble importing a TypeScript module from the global node_modules directory

I have a library folder located in the global node modules directory with a file named index.ts inside the library/src folder //inside index.ts export * from './components/button.component'; Now I am trying to import this into my angular-cli ap ...

Issue with the RxJS async pipe not functioning in a Reactive Development environment

Currently, I am in the process of learning reactive development in Angular by transforming my old methods in a service to work without subscribing at all in an Observable. So far, I have successfully converted the getAll method to this new approach. Now, m ...

Different ways to update the AutoComplete Style attribute

MuiInput-formControl { margin-top: 16px; Is there a way to reset the marginTop property to zero? I attempted the following method, but it was not successful. MuiFormControlLabel: { marginTop: 0, }, <Autocomplete disabl ...

Mapped types: Specify mandatory properties depending on whether an array of identical objects includes a specific string value

Can an object property be set to required or optional based on the presence of a specific string in an array within the same object? type Operator = "A" | "B" type SomeStruct = { operators: Operator[]; someProp: string; // this should be ...

There is no universal best common type that can cover all return expressions

While implementing Collection2 in my angular2-meteor project, I noticed that the code snippets from the demo on GitHub always result in a warning message being displayed in the terminal: "No best common type exists among return expressions." Is there a ...

How can I incorporate an iframe into Angular while utilizing a value fetched from MongoDB as the src attribute?

When using an iframe similar to this: <iframe class="e2e-iframe-trusted-src" height="480" width="500" src={{trailer.address}}> </iframe> I encountered the following error: "ERROR Error: unsafe value used in ...

Guide to publishing with Chromatic in a Storybook Angular NX workspace

Hey there, I'm looking for some help with running Storybook Chromatic in an NX workspace. My Angular application and libraries are functioning properly when served locally, and I've been able to run stories for my projects without any issues. I ...

Load a React component in the same spot when clicked

Is there a way to implement a functionality where clicking on different buttons in a panel will display different components in the same location? <AddNotification /> <EditNotification /> <DeleteNotification /> const AdminPanel = () =& ...

Issue with music in Phaser3 game not playing correctly when transitioning to a new scene

I'm completely new to Phaser and I've encountered an issue with adding a start menu to my main platformer scene. The gameplay scene plays music seamlessly, but when I introduce a start menu, things start to go wrong. Here's a snippet of the ...

Discover the power of Angular 4 with its *ngFor loop constraints, and enhance user interaction

<ul> <li *ngFor="let hero of heroes | slice:0:10;"> {{ hero.name }} </li> </ul> <p *ngIf="heroes.length > 10" (click)="showMore()">show more 10</p> I am looking to display additional hero names when clic ...

Obtaining JSON data in an Angular service: A beginner's guide

My JSON file has the following structure: { "user": [ { "id": 0, "data": [ { "userName": "iheb", "useremail": "", "userPassword": "kkk" } ], "questionnaireListe": [ { ...

Error: TypeScript unable to locate file named "Image"

I'm encountering an issue while attempting to construct a class for loading images. The error message states that name "Image" not found within the array definition, even though I create an image object later in the code. class ImageLoad ...

Is there a way to ensure that in angular 2+, when clicking on a button with the same route as the current page, the page will reload?

Why would I even consider complaining about this? You’re already here, aren’t you? I have a couple of reasons why a reload needs to happen, but for now I’ll just mention one. 1) When my user clicks on a link, they may not realize it's the same ...

What is the best way to configure eslint or implement tslint and prettier for typescript?

In my React/Redux project, I recently started integrating TypeScript into my workflow. The eslint configuration for the project is set up to extend the airbnb eslint configurations. Here's a snippet of my current eslint setup: module.exports = { // ...

Angular2 - HTML not displaying the response

I am currently mastering angularjs2. In my latest project, I attempted to fetch data from an API and received a response successfully. However, I encountered an issue where the response is not rendering in homepage.component.html as expected. I am unsure o ...

Leveraging NestJs Libraries within Your Nx Monorepo Main Application

I am currently part of a collaborative Nx monorepo workspace. The setup of the workspace looks something like this: https://i.stack.imgur.com/zenPw.png Within the structure, the api functions as a NestJS application while the data-access-scripts-execute ...

Is there a way for me to access and install the Angular 2 release candidate through either npm or

When attempting to download Angular2 from npm or jspm, I am encountering an issue. Instead of getting the version 2.0.0-rc.1, I am receiving angular@^2.0.0-beta.17. Could this discrepancy be related to changes in the release candidate or it is a matter of ...

Why are polyfills-es2015, vendor-es2015, main-es2015, and other files blocked from loading because the MIME type is not allowed (text/html

Hey there! I've encountered an issue during the deployment of my Angular app. When I serve it from Angular locally, everything works perfectly fine. However, when I deploy it on a Tomcat server, I encounter errors and I can't see anything related ...

Managing asynchronous requests with RxJS for time-spaced dependent API calls

I am currently working on developing a code in Angular 11 to handle the following scenario: I have a list of files, and for each file, I need to make an API call (let's say api1) to retrieve a fileId from the response. Subsequently, I will use this f ...

The parameters 'event' and 'payload' do not match in type

Upon running the build command, I encountered a type error that states: Type error: Type '(event: React.FormEvent) => void' is not assignable to type 'FormSubmitHandler'. Types of parameters 'event' and 'payload&apos ...