Issue with Angular ngFor: displaying only one name instead of all names in the loop

In my component.ts file, I have retrieved this data from an API call:

[ 
    [ 
        { 
            "name": "name one",
            "desc": "something here",
        },
        {
            "name": "name two",
            "desc": "something here",
        },
        { 
            "name": "another name",
            "desc": "something here",
        }
    ]
]

I then assign it to my component.html using the following code:

this.mydata = [result];

The array [result] contains the aforementioned data.

Next, I implement a select element:

<select name="list">
    <option *ngFor="let data of mydata; value="{{ data.name }}">{{ data.name }}</option>
 </select>

However, the issue I am facing is that the select element does not display any names.

To troubleshoot, I added the line: {{ mydata | json }} and confirmed that the data is indeed present as shown above.

How can I resolve this problem?

Answer №1

Seems like you are introducing an additional array wrapper []. As a result, the output appears as [[...]]. I recommend eliminating the unnecessary brackets here:

this.mydata = result; // <---the actual data could resemble this format = [{}, {}, ...]

Answer №2

The information you have is structured in a nested array format. This means that when you loop through the myData property, it contains another array without any references to name and desc. Give this a try:

this.mydata = result[0];

Answer №3

There seems to be an issue with assigning the mydata variable. At the moment, the value of mydata is identical to the value of result. To rectify this, make the following update in your code:

this.mydata = result[0];

Alternatively, you can try the following approach:

const [result] = [
    [
        {
            "name": "name one",
            "desc": "something here",
        },
        {
            "name": "name two",
            "desc": "something here",
        },
        {
            "name": "another name",
            "desc": "something here",
        }
    ]
];
this.mydata = result;

After making these changes, display the data in your Angular code.

<select name="list">
    <option *ngFor="let data of mydata;" value="{{ data.name }}">{{ data.name }}</option>
</select>

Note that there appears to be a missing double-quote when defining *ngFor in your question's context.

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

What are the compatibility considerations for npm packages with Angular 2? How can I determine which packages will be supported?

When working with Angular 2, do NPM packages need to be modified for compatibility or can any existing package work seamlessly? If there are compatibility issues, how can one determine which packages will work? For instance, let's consider importing ...

Guide to removing selected value from a combobox in Angular

I am working on a simple HTML file that consists of one combobox and one clear button. I want the clear button to remove the selected value from the combobox when clicked. Below is my code: mat-card-content fxLayout="row wrap" fxLayoutAlign="left" fxLayou ...

Show all span elements in a map except for the last one

Within my ReactJS application, I have implemented a mapping function to iterate through an Object. In between each element generated from the mapping process, I am including a span containing a simple care symbol. The following code snippet demonstrates t ...

Remove the export statement after transpiling TypeScript to JavaScript

I am new to using TypeScript. I have a project with Knockout TS, and after compiling it (using the Intellij plugin to automatically compile ts to js), this is my sample.ts file: import * as ko from "knockout"; ko; class HelloViewModel { language: Kn ...

Issue with Nuxt2 CompositionAPI: Unable to showcase imported render function in component - Error message states "template or render function not defined"

I have created a render function that I believe is valid. I am importing it into a component and registering it within the defineComponent. However, when running the code, I encounter an error saying "template or render function not defined". I am confide ...

What is the best way to utilize the typescript module for detecting and managing typescript errors and warnings in your code?

Currently, I am experimenting with the typescript module to programmatically detect typescript errors. Below is a simplified version of what I have been working on: var ts=require('typescript') var file_content=` interface Message{ a:string ...

Angular 4 - Issues with route configurations

My Angular application is running smoothly on localhost:4200 using ng serve. The node server can be found at localhost:3000. After running ng build, a bundle file is generated and properly served from localhost:3000 thanks to the line app.use(express.sta ...

Question from a student: What is the best way to transfer information between different classes?

Currently, I am delving into SPFX development. My focus is on constructing a form that incorporates multiple classes in order to gain insight on how they can interact and share data among one another. In this scenario, I have established two distinct clas ...

In TypeScript, a mapped type is not allowed to define properties or methods

My challenge involves defining an interface with keys that match a specific enum key type. However, when I attempt to declare this type, I encounter the following error message: A mapped type may not declare properties or methods. Below is the code snip ...

Ways to confirm the visibility of a web element on the screen with serenity-js

In the current project, I am utilizing the Serenity-js BDD framework with a screenplay pattern approach. However, I am encountering an issue when attempting to assert the visibility of an element on a web page using the "that" method from the Ensure class. ...

How can interfaces be effectively integrated with node and mongoose?

I am working on a model interface where I need to fetch specific data from the record // file: code.interface.ts import { Document } from 'mongoose'; export interface CodeI extends Document { readonly _id: string; readonly logs: any; } Howe ...

Subscribing to ngrx store triggers multiple emissions

Currently, I have an app with a ngrx store set up. I am experiencing an issue where, upon clicking a button, the function that fetches data from the store returns multiple copies of the data. Upon subsequent clicks, the number of returned copies grows expo ...

Clicking the "X" close button on a mat-list removes an item from the user interface

Recently, I utilized the mat-list component to display items on the UI. Each item is accompanied by a close(X) button located on the right-hand side. https://i.stack.imgur.com/1E2ZG.png Html Code <mat-list > <ng-container *ngFor='le ...

Angular setup prompting for anonymous usage statistics collection by the Angular development team

This is my first time installing Angular and I ran into confusion at this step. The terminal message prompted me with the option to share anonymous usage data with the Angular Team at Google under their Privacy Policy. Here's what it said: ? Would y ...

Using p5.js with TypeScript and Webpack is not supported

I'm currently working on a library project that involves utilizing p5.js. Specifications Here is a snippet of my Webpack configuration: const path = require('path'); module.exports = { entry: './start.ts', output: { ...

Having trouble determining the total amount in my online shopping cart

I am facing an issue with the shopping cart I created, which consists of two components (Productlist and Cart List). When I click on the 'add to cart' button in the Product List, it successfully moves into the service file and then to the Cart Li ...

What is the best way to arrange map elements in a React application?

I am trying to implement filter buttons for low to high rates and high to low rates, but I am having trouble figuring it out. How can I apply the filter to display the data accordingly? The data that needs to be filtered is stored in rate.retail_rate. ty ...

Having Trouble with Imported JavaScript File in Astro

Why isn't the js file working in Astro when I try to import or add a source in the Astro file? For example: <script src="../scripts/local.js"></script> or <script>import '../scripts/local.js'</script> I am ...

Does the React memo function modify the component's prop type?

I've come across a strange issue where defining two components causes compilation errors when written separately but not when written in the same file. test3.tsx import React from "react"; type ValueType = number[] | string[] | number | st ...

Adjustable Material UI Switch that reflects the value, yet remains changeable

I am facing a challenge with integrating a Material UI switch (using Typescript) to showcase a status value (active/inactive) on my edit page, and making it toggleable to change the status. I have been able to either display the value through the switch OR ...