Utilizing a Map with Angular's ngFor

Currently, I am working with Ionic 3, which utilizes Angular and TypeScript.

My goal is to use ngFor with a Map type in my project.

Below is what I have implemented so far:

interface IShared_Position {
    lat: number;
    lng: number;
    time: number;
    color?: string;
}
public shared_position = new Map<string, IShared_Position>();

Here is the HTML code related to this:

<ion-list>
  <ion-item ion-item *ngFor="let item of shared_position">
    <h2>{{ item.time }}</h2>
  </ion-item>

However, I encountered the following error message:

Error: Cannot find a differ supporting object '[object Map]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Is there a way to bind my map even if it's not iterable?

How should I proceed in this situation?

Answer №1

<div *ngFor="let element of elements | keyValue">
{{element.key}}...{{element.value}}
</div>

Answer №2

When you only require the values, you can simply iterate over them

html:

<ion-item ion-item *ngFor="let item of getValues()">
    <h2>{{ item.time }}</h2>
</ion-item>

ts:

getValues(): Array<IShared_Position> {
    return Array.from(this.shared_position.values());
}

If you need both keys and values, you can convert the Map into an iterable object containing arrays with the key at index 0 and value at index 1 using the entries function:

html:

<ion-item ion-item *ngFor="let item of getEntries()">
    <h2>key: {{ item[0] }} - time: {{item[1].time}}</h2>
</ion-item>

ts:

getEntries(): [string, IShared_Position] {
    return Array.from(this.shared_position.entries());
}

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 is the process for including a resource parameter in the acquireTokenSilent method of an MSAL instance within an Angular application?

const requestToken = { permissions: ['view_only'], }; newToken = await this.authInstance.acquireTokenSilent(requestToken); I'm trying to include the client ID of my Web API as a resource parameter when requesting the access token. Strugg ...

Establish a Svelte and TypeScript "observe" script for developing vscode extensions

Hi there! As a newbie in TypeScript, Svelte, and VSCode extension development, I have my first question to ask ...

What is preventing me from retrieving a value from a member function or method within a TypeScript class instance?

I am facing an issue with the FileInfo class that implements the IFileInfo interface. This class has an instance member function ext and a function getExt(). Within my component, there is a private method named openTempFolder() which makes an HTTP call to ...

Repeated calls to Angular's <img [src]="getImg()"> frequently occur

Can someone help me figure out what's going on here? Recently, I set up a new Angular project and in app.component.html, I have the following code: <img [src]="getDemoImg()"> In app.component.ts: getDemoImg(){ console.log('why so m ...

Retrieving child elements from parent identifiers using Typescript

I've been working on creating a new array with children from the data fetched from my database. While my initial attempt was somewhat successful, I believe there are some missing pieces. Could you assist me with this? Here is the raw data retrieved f ...

Disabling end date options in an Angular Material date range picker based on the selected start date

I am currently using an Angular Material 15 date range picker and it is functioning properly. I can select a start date and an end date, which are then displayed in the associated input fields. However, I am facing an issue with setting the minimum and m ...

What is the best way to retrieve the most recent entry in a Firebase real-time database?

Utilizing Firebase's real-time database, I am updating values in a chart as they change. However, my struggle lies in retrieving only the most recent value added to the database. While browsing through limitToLast and 'child_added' do not w ...

Retrieve information from two distinct observables from within the resolver function

I'm facing an issue with my services that work on the observable principle. I am trying to fetch the results of 2 services inside a resolver to display on my page. However, the data being displayed on the page is just an empty data object. I even trie ...

Dealing with asynchronous data in ngOnInit in Angular 4 when routing with parameters: tips and tricks

I'm currently facing an issue with loading data from my web api controller. The problem lies in the fact that I am using my API service, which is invoked from the ngOnInit function of the component. However, the view remains empty as the data retrieva ...

Navigating through a JSON dictionary in Svelte: A step-by-step guide

When working with Svelte, the #each construct allows for easy iteration over array-like objects. But what if you have a JSON dictionary object instead? Is there a way in Svelte to iterate over this type of object in order to populate a dropdown menu? The ...

Looking to implement nested routes in Angular to ensure that each component's view updates properly

app-routing.module.ts import { NgModule } from '@angular/core'; import { PreloadAllModules, RouterModule, Routes } from '@angular/router'; import { BreadcrumbComponent } from './main-layout/breadcrumb/breadcrumb.component'; im ...

Child class in TypeScript lacking type information for abstract property

Having an issue with my TypeScript 3.4 code that seems a bit strange. Here's a snippet of the problematic code: interface MyInterface { fn: (x: number) => number; } abstract class A { abstract prop: MyInterface; } class B extends A { prop ...

Why is it that the angular app is able to access the server, but the web API is unable to be accessed when called from an external system? It seems to work

Today I dedicated a solid 10 hours trying to resolve an issue, but unfortunately, I am still stuck. I am fairly new to angular, webapi, and deployment processes. If anyone out there can offer some assistance, it would be greatly appreciated. Here's ...

What is the process for integrating mobiscroll into an Angular project using a customized username?

After installing the mobiscroll trial in my angular project, I decided to reinstall it with a new username. Despite attempting to remove the library and using the command "mobiscroll config angular", I encountered an issue where it was still logging in w ...

The successful operation of 'Ionic serve --lab' may involve the need to manually save the app.module

We are currently working on an Ionic project that consists of several pages and a single custom provider named request.ts. The issue we are facing is that whenever we launch it using the command ionic serve --lab, the compilation fails (with the error poin ...

The click listener triggers a single time when a render method is nested within it

When I have a click listener on a button that resets the innerHTML of a div with a render method, the listener fires every time I click if I take out the render function. However, if the render function is included, the listener does not fire multiple time ...

Mapping type property names in Typescript for substitution

I am working on a function that accepts two objects as parameters: one representing a model and the other representing a mapping. The function then returns an object with two properties: one showing the base model and the other displaying the model with ea ...

Limit file upload size to less than 1MB in Angular 2 with typescript using ng2-file-upload

Having issue with my code - I can't upload a file larger than 1mb even though maxFileSize is set to 50mb. Can anyone help me troubleshoot? @Component({ moduleId: module.id, selector: 'NeedAnalysisConsult', templateUrl: 'nee ...

Guide to Setting Up "Remember Me" Login for Users in Angular

I am having trouble setting the 'Remember Me' option on my Login page. I tried using localstorage session but it seems like something is missing in the service file that's causing it not to respond properly. I attempted to follow a guide on ...

The Java AWS Lambda function is returning an empty JSON response in the body

I am utilizing the Angular application https://github.com/Kyro1980/timeout-app-public and integrating the Payment Intents API for complex payment flows with Stripe. In this process, I send a PaymentIntent (Stripe) to AWS Lambda and attempt to fetch a respo ...