Attempting to showcase information on the Angular frontend

When attempting to retrieve the Street name, I am seeing [object Object]. What is the optimal approach for displaying JSON data on the client side?

I managed to display a street name but struggled with other components. How can I access the other elements?

//Interface 

export interface Street {
    name: string;
    color: string;
}

export interface Place {
    name: string;
    streets: Street[];
    stopLights: string[];
    similarStreets: string[];
}

// Client Side
<h2>Place List</h2>
<ul *ngFor="let place of places.places">
  <li>{{place.name}}</li>      //This works and displays the name
  <li>{{place.color}}</li>    //showing [object Object] for color

</ul>

//JSON Data 
{
  "places": [
    {
      "name": "San Jose",
      "street": [
        {
          "name": "1st Street",
          "color": "Sharkish"
        },
        {
          "name": "Santa Clara",
          "color": "49ers"
        }
     ],
  }

Answer №1

implement a separate *ngFor directive for displaying colors

<h2>List of Places</h2>
<ul *ngFor="let place of places.places">
  <li>{{place.name}}</li>
  <li *ngFor="let street of place.street">{{street.color}}</li>
</ul>

Answer №2

When trying to display the "color" attribute of the Place object within the Street object, it seems that there is a mismatch between the Place interface and your JSON Data. The interface has "streets" in plural form while the JSON data does not.

To resolve this issue, adjust your JSON data structure as follows:

//JSON Data 
{
  "places": [
    {
      "name": "San Jose",
      "streets": [
        {
          "name": "1st Street",
          "color": "Sharkish"
        },
        {
          "name": "Santa Clara",
          "color": "49ers"
        }
     ],
  }

Once you have made these changes, you can use the following code snippet:

<li *ngFor="let street of place.streets">{{ street.color }}</li>

This will allow you to access the color attribute of each street within your place object.

Answer №3

Urban area is a collection of text values. Consequently, in order to retrieve the item contained within it, you must employ an additional iteration (*ngFor) to iterate over each element.

Answer №4

Appreciate all the help! I was able to find a solution by rolling up my sleeves and diving in. Here's another example!


   <div>
            <ol>
                 <li *ngFor="let item of videoList" > 
                      <div>{{item.title}}</div> 
                      <ol>
                           <li *ngFor="let subItem of item.nodes"> 
                                <div>{{subItem.title}}</div> 
                           </li>
                      </ol>
                 </li>
            </ol>
       </div>


<h2>List of Places</h2>
<ul *ngFor="let place of places.places"> 
  <li>{{place.name}}</li>      //This is functional and displays the name
  <div *ngFor="let myStreet of place.street"> 
     <li>{{ myStreet.name }} </li>
  </div>
</ul>

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

Step-by-Step Guide on Resetting Versions in Package.json

I currently have around 20 react projects, each with their own package.json files. These package.json files contain various packages and their versions: "@material-ui/core": "4.11.4", "@material-ui/icons": "4.11.2", ...

What is the best way to determine if an object.property is defined in Angular 2?

Is there a similar function in Angular 2 to angular.isDefined from Angular 1? I have tried using the safe navigation operator ?., which is only supported in templates. ...

Issue with dynamic form JavaScript functionality after removing curly braces { } from a select tag in Rails

In my Rails form, there is a gender field defined as follows: <%= f.select :gender, ["Male","Female"],{class: "gender"} %> I also tried adding an onclick event like this: <%= f.select :gender, ["Male","Female"],{class: "gender"},onclick: "categ ...

Real-time Broadcasts Straight to Your Web Browser

Feeling a bit frustrated with my current situation. I am eager to stream a live video broadcast to a web browser. At the moment, I am utilizing ffmpeg to stream a directshow live source as a webm stream to node.js, which then sends the stream to the http ...

Having trouble setting up react-i18n with hooks and encountering a TypeError: Cannot read property '0' of undefined?

Encountering an error while setting up the react-i18n with hooks: TypeError: Cannot read property '0' of undefined Here's the content of i18n.js: import i18n from 'i18next'; import { initReactI18next } from 'react-i18next/h ...

Iterate through an HTML table and transfer matching items along with their corresponding calculated amounts to a separate table

<html> <body> <div> <table border="1" id="topTable"> <thead> <th>Item</th> <th>Sold</th> </thead> <tbody id="topTableBody"> <tr> ...

Using JSON in an AJAX request to log in

Currently, I am in the process of developing a straightforward login form that utilizes AJAX for server communication and PHP as the server-side script. However, I have encountered some challenges while trying to send login data to the server via JSON. Th ...

When using json_decode, it will return an array containing only 1 element

I'm currently facing an issue with decoding some JSON data into a PHP array. Below is the code snippet in question: $getfile='{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}'; $arr = json_decode($getfile, t ...

The difference between `$(document)` and `$("document")` is like night

Does a distinction exist between: $(document) and $("document")? ADJUSTMENT: also when using the .ready() method like $("document").ready() ...

utilizing gson to parse multiple JSON objects within a JSONArray

Struggling to parse this result array with Gson for the past two days. I've read advice on SO and other sources suggesting the need to define a top-level container, but I'm unsure how to complete its definition. results:[ { "SupplierCatalog": ...

The functions that have been imported are not defined

I encountered a Error in created hook: "TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.$_playerApi_getPlayers is not a function" issue while using Webpack through Vue CLI on my webpage. Below is the structure of my project directory: . + main.js + ...

Error: Unable to use 'ngForFrom' as it is not a recognized native property for binding

I have looked for solutions here and here. I am currently using angular 2 version 2.0.0-beta.7, but encountering the following error: EXCEPTION: Template parse errors: Can't bind to 'ngForFrom' since it isn't a known native property (" ...

"Error: The update depth has exceeded the limit while trying to use the

I've been working on implementing localStorage in NextJs using TypeScript by following this guide at , but I am encountering an error. https://i.sstatic.net/NX78a.png Specifically, the error occurs on the context provider repeatedly. Below is the c ...

comparative analysis: nextjs getServerSideProps vs direct API calls

Trying to grasp the fundamental distinction between getServerSideProps and utilizing useSWR directly within the page component. If I implement the following code snippet in getServerSideProps: export const getServerSideProps = async () => { try { ...

Show a specific item when selecting an option from a dropdown menu

Hey there, I have a question regarding creating a theme for the Genesis Framework. Right now, I'm facing a challenge with one particular element. Here's the situation: I've got two drop-down lists, but I only want the second one to be visib ...

The Battle of Extends and Intersection in Typescript

Typescript's concept of extension is akin to C++'s inheritance. Intersection in Typescript involves creating a new object with all the properties from the intersected classes. Why utilize intersection when extends keyword can already merge ...

What is the Best Way to Enable Tooltips to Function from External Elements?

I am currently designing a map that features points with tooltips. When hovered over, the tooltips function correctly. I am interested in exploring the possibility of making the tooltips interact with an external navigation bar. My goal is to have specifi ...

Issue with scroll down button functionality not functioning as expected

Is there a way to create a simple scroll down button that smoothly takes you to a specific section on the page? I've tried numerous buttons, jQuery, and JavaScript methods, but for some reason, it's not working as expected. The link is set up co ...

Transform URLs to end with .jpg using NextJS

My API has a specific endpoint called /api/thumbnail that currently returns a JPEG image. However, I would like the endpoint to also accept .jpg, so it can be accessed as /api/thumbnail.jpg. Would I be able to achieve this using only pure NextJS code, or ...

Struggling to run the CommandLineRunner in springboot. I am encountering issues with reading JSON data using the following code. Any suggestions on

Encountering an issue with Java version 11. The code seems error-free during compilation, but at runtime, the error "Failed to execute CommandLineRunner" is displayed. I have correctly added a JSON file to the resource directory, yet when trying to access ...