Issue with Angular 6 where data is not binding to the UI on initialization

I am struggling with binding dynamic data to the UI and Data tables on my website. Despite trying various methods, I have not been able to achieve success. Currently, I am using the smart admin latest theme for development.

When I make a call to the API, I do receive a response.

var tabledata:any[]=[]
Get(){
    this.getservice().subscribe(
        res=>{
            if(res && res.data && res.data.length>0){
                this.tabledata=res.data;
                console.log(this.tabledata);
            }
        }
    )
}

In the HTML:

 <tr *ngFor="let data of tabledata">
        <td>{{data.name}}</td>
        <td>{{data.age}}</td>
    </tr>

Answer №1

Have you considered running your Get method in the OnInit function? What is the reason for starting your Get method with a capital letter? Is there a specific reason why you are declaring your table as a table of any type? It's usually better to use typed objects. Could you test the following code snippet below?


    let tableData: Person[];

    class MyComponent implements OnInit {

        ngOnInit() {
            this.get();  
        }

        get(){
            this.getService().subscribe(
                res => {
                    this.tableData = res.data;
                    console.log(this.tableData);
                }
            )
        }
    }

I recommend adding *ngIf in front so that the table is displayed only after the data has been loaded.


<div *ngIf="tableData">
    <tr *ngFor="let data of tableData">
        <td>{{data.name}}</td>
        <td>{{data.age}}</td>
    </tr>
</div>

If even after this, there is no data being displayed, you can try:


<div *ngIf="tableData">
    <tr *ngFor="let data of tableData">
        {{data | json}}
    </tr>
</div>

It appears that the data object you are receiving may not contain attributes like name and age.

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

Restrain a Key according to the data type of its value within a universal category

I am currently working on creating a versatile function where the generic type is used to define its parameter. Here's an excerpt from this parameter : type Configuration<T> = { masterdata: T[], target: ???? } I am encountering difficu ...

What is the best way to pass the "getjson" capability from one function to another in a seamless manner?

Currently, I am dealing with a situation where a getjson call is used to retrieve a large amount of data. However, I find myself constantly having to check if the data is null or not. Here is an example: if (data.Height == "") { $(&ap ...

Animate moving between two points over a set period using JavaScript

My attempt to smoothly move a box from one position to another using Linear Interpolation is not working as expected. The box just moves without any easing effect. I came across a post on Stack Overflow titled C# Lerping from position to position, but I ma ...

Angular 8 Issue: Absence of _body and Headers in Response

Our back-end code is written in C# within the .NET environment, targeting the 4.6.1 framework. Recently, our front-end was upgraded from Angular 4 to Angular 8. During this upgrade, webpack transitioned from version 2.3 to version 4.41 and typescript from ...

Obtaining an XML element within an XSLT transformation

I need to retrieve the value of an XML element within XSL. Although my JavaScript is able to select one XML document, there are elements in other documents that I also require. I am uncertain if this task is feasible or if my syntax is incorrect. This is ...

Javascript is unable to access the occupied value. Error: Unable to read property 'weight' of undefined

I encountered a strange issue that I wasn't expecting. The snippet of code below represents a simple function. However, the problem arises when I try to access 'truckArray[0].weight' and receive an error stating TypeError: Cannot read prop ...

JavaScript issue: "Error: Uncaught (in promise) SyntaxError: Unexpected end of input"

Encountering the following error message: Uncaught (in promise) SyntaxError: Unexpected end of input when attempting to post references to my specific express server. Can anyone here offer assistance? function create() { event.preventDefault() fi ...

When running the npm install command for Angular, an error occurred stating: "npm ERR! Maximum call stack

Upon running npm install, I encountered the following message: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2256510f514b4f524e470f4351566213100c160c12">[email protected]</a>: NOTICE: ts-si ...

Error message: "The term 'Outlet' is not recognized in react-router version 6.4"

What is the proper way to define the Outlet component in react-router version 6.4? Below is a code snippet: function Layout() { return ( <div> <Navbar /> <Outlet /> <Footer /> </div> ); } ...

Emphasize a specific line of text within a <div> with a highlighting effect

I'm looking to achieve a similar effect as demonstrated in this fiddle As per StackOverflow guidelines, I understand that when linking to jsfiddle.net, it's required to provide some code. Below is the main function from the mentioned link, but f ...

Attempting to modify Namecheap's custom DNS field through Python Selenium

I am facing an issue while attempting to modify DNS settings for domains in Namecheap using Python Selenium Below is the HTML code: <select class="dashed-select add-margin ng-untouched ng-valid select2-offscreen ng-dirty ng-valid-parse" ng-change="nam ...

Having trouble passing data from router to View with EJS in Express. Despite trying, still encountering ReferenceError message

I encountered an issue when trying to display form errors to the user. I attempted to pass the errors from the router to my view file. Error Message ReferenceError: e:\2016\passport\views\register.ejs:38 36| 37| <div ...

Guide to activating the isActive status on a live link within a map iteration utilizing the NEXTUI navigation bar

Check out the new NEXTUI navbar I'm using: I am having trouble setting the isActive property on the active link in my NavBar component in Next.js. I couldn't find much help on Google, so I'm hoping someone here has experience with this or k ...

Mastering the art of nesting loops and conditions in React: A comprehensive guide

I am fetching data in React using the following method: const data = [ { header: 'my header1', copy: [ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Etiam et risus quam. Pr ...

Unable to send event from JavaScript to Component in Ionic

My goal is to notify a component that a script file has finished loading. My approach involves using the onload event of an element to dispatch an event. A service will then register an event listener for this event, waiting for clients to subscribe to an ...

The connection to Socket IO was refused due to a network error (net::

As I work on integrating socket.io into my application hosted on Azurewebsites, webapp I have the following server.js setup: var app = require('express')(); var server = require('http').createServer(app); server.listen(process.env.PO ...

Ways to reach the Document Object Model in a functional component

While working on a speedometer project in ReactJS with some vanilla syntax, I encountered an issue where the canvas element was returning null. Oddly enough, when running const canvas = document.getElementById('dial__container'); in the console, ...

Transforming a JSON file that has been previously converted to an Observable into a TypeScript map within an Angular application

There is a json data file named dummy, with the following structure: [ {"key":"KEY1", "value":["alpha","beta","gamma"]}, {"key":"KEY2", "value":["A","B","C"]}, {"key":"KEY3", "value":["One","Foo","Bar"]} ] The goal is to convert this json f ...

Using Rxjs to reset an observable with combineLatest

My scenario involves 4 different observables being combined using "combineLatest". I am looking for a way to reset the value of observable 2 if observables 1, 3, or 4 emit a value. Is this possible? Thank you! Mat-table sort change event (Sort class) Mat- ...

Is there a way to effectively alter an object that has been assigned in a separate file?

Seeking Assistance: I am facing an issue in my current project where I need to store a javascript object in an external file and then export it using module.exports. The challenge now is that I want another file to be able to modify a specific value withi ...