Navigating an array using ngFor and encountering an error message saying, "Identifier not specified"

While using ngFor to iterate through an array, I encountered the following error message:

"Identifier 'expenseitem' is not defined. The component declaration, template variable declarations, and element references do not contain such a member"

I have verified that the identifier is indeed defined in the imported model.ts file. What could be causing this issue?

receipts.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>Expenses Total: {{ totalExpenses | currency:'GBP'}}</ion-title>
    <ion-buttons slot="primary">
      <ion-button routerLink="/addform">
        <ion-icon name="add" slot="icon-only"></ion-icon>
      </ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>

<ion-content>
<ion-grid>
  <ion-row>
    <ion-col size="12" size-sm="8" offset-sm="2" text-center>
      <ion-card>
        <ion-card-header>
        <ion-card-title>
          {{ loadedExpenses[0].timestamp }}
        </ion-card-title>
      </ion-card-header>
      <ion-img [src]="loadedExpenses[0].imageUrl"></ion-img>
      <ion-card-content>
        <p>{{ loadedExpenses[0].amount | currency:'GBP' }}</p>
      </ion-card-content>
      </ion-card>
    </ion-col>
  </ion-row>
  <ion-row>
    <ion-col size="12" size-sm="8" offset-sm="2" text-center>
      <ion-list>
        <ion-item *ngFor="let expenseitem of loadedExpenses"></ion-item>
        <ion-thumbnail slot="start">
          <ion-img [src]="expenseitem.imageUrl"></ion-img>
        </ion-thumbnail>
        <ion-label>
          <h2>{{ expenseitem.timestamp }}</h2>
          <p>{{ expenseitem.amount }}</p>
        </ion-label>
      </ion-list>
    </ion-col>
  </ion-row>
</ion-grid>
</ion-content>

receipts.page.ts

import { Component, OnInit } from '@angular/core';

import { ExpensedataService } from '../expensedata.service'
import { Expenseitem } from '../expenseitem.model';

@Component({
  selector: 'app-receipts',
  templateUrl: './receipts.page.html',
  styleUrls: ['./receipts.page.scss'],
})
export class ReceiptsPage implements OnInit {
  loadedExpenses: Expenseitem[];

  constructor(private expensedataService: ExpensedataService) { }

  ngOnInit() {
    this.loadedExpenses = this.expensedataService.expenseitems;
  }

}

expenseitem.model.ts

export class Expenseitem {

    constructor(public id: string, public timestamp: number, public amount: number, public imageUrl: string
        ) { }
}

Answer №1

I managed to resolve the issue.

I realized my mistake was with the incorrect placement of the closing </ion-item> tag:

<ion-item *ngFor="let expenseitem of loadedExpenses"></ion-item>
        <ion-thumbnail slot="start">
          <ion-img [src]="expenseitem.imageUrl"></ion-img>

It became clear that I needed to iterate within the same tag for it to function correctly. By adjusting the position of </ion-item> to encompass my iteration, everything now works as intended.

<ion-item *ngFor="let expenseitem of loadedExpenses">
        <ion-thumbnail slot="start">
          <ion-img [src]="expenseitem.imageUrl"></ion-img></ion-item>

A sincere thank you for your assistance and I apologize for any inconvenience caused!

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

Even as I create fresh references, my JavaScript array object continues to overwrite previous objects

Coming from a background in c# and c++, I am facing some confusion with a particular situation. Within the scope of my function, I am creating a new object before pushing it into an 'array'. Strangely, when I create the new object, it appears to ...

The CORS policy has blocked the Vimeo URL from its origin, stating that the PATCH method is not permitted according to the Access-Control-Allow-Methods preflight response

Using Angular for web development. When uploading a video to Vimeo, it involves 3 steps: Create the video. Upload the video file. Verify the upload. The process of creating a video is successful, however, encountering an error during the vid ...

The mystery of the undefined return value in my Ionic v4 get function

I attempted to retrieve my location by saving the latitude and longitude, but my declared variable isn't returning anything. Take a look at my code snippet: public device_location: any = {}; constructor(private geolocation: Geolocation) { this.s ...

"Discover the power of Next.js by utilizing dynamic routes from a curated list

I am working on a Next.js application that has a specific pages structure. My goal is to add a prefix to all routes, with the condition that the prefix must be either 'A', 'B', or 'C'. If any other prefix is used, it should re ...

Is there Polyfill Compatibility for Custom Elements in Angular 9?

When it comes to polyfilling support for custom elements created with Angular, there are various recommendations available. This demo demonstrates that adding the following polyfill in polyfills.ts works: import '@webcomponents/webcomponentsjs/custo ...

Tips for integrating a custom handler to the close icon in Material UI TextField component

In my Reactjs/Typescript project using Material UI, I have a search input component rendered with TextField. The built-in "x" icon clears the input value, but I want to create a custom handler for making an API call when the search value is deleted. I&apo ...

What is the best way to select types conditionally based on the presence of a property in another type?

To begin with, I have a specific desired outcome: type Wrapper<ID extends string> = { id: ID }; type WrapperWithPayload<ID extends string, Payload> = { id: ID, payload: Payload }; enum IDs { FOO = "ID Foo", BAR = "ID Bar", BAZ = "ID Baz ...

Implementing modifications to the @Input value in Angular components

Recently, I started learning Angular and TypeScript. I've been working with a component that accepts parameters. Here's an example: <app-measure-card date={{item.date.substring(0,11)}} type={{item.type}} ...

Universal in Angular is malfunctioning

As a newcomer to Angular 4, I am looking to create an Angular app that is SEO friendly and supports Angular Universal (with the --universal flag after ung new or ung init). I have successfully created an Angular Universal app. However, when running the p ...

Combine data from a new observable with the existing data in Angular using RxJS

I have a list stored as an observable[]. To subscribe to it in the template, I am using async. Each time I scroll, this method is called: onScroll() { this.list$ = this.list$?.pipe( tap((list) => { this.notificationService . ...

Prevent all dates from being selected except for the last day of every month in the Angular Material Calendar component

Is it possible to restrict all dates except the final day of each month with Angular Material? I need users to only be able to select this particular date. There is a [matDatepickerFilter] property, but it seems to work only for a specific list of dates. ...

Despite encountering the 'property x does not exist on type y' error for Mongoose, it continues to function properly

When working with Mongoose, I encountered the error TS2339: Property 'highTemp' does not exist on type 'Location' when using dot notation (model.attribute). Interestingly, the code still functions as intended. I found a solution in the ...

Experiencing an issue with my Angular 6.1.0 setup, using angular-cli 7 and primeng 7 - specifically encountering the error message "Initializers are not allowed in ambient context."

Issue encountered in the primeng package: While examining node_modules/primeng/components/picklist/picklist.d.ts, errors were found at line numbers 65 and 66. I have investigated the primeng package further. primeng/components/picklist/picklist.d.ts l ...

How can I dynamically insert a variable string into a link tag using React and TypeScript?

I am just starting out with javascript and typescript, and I need to generate a link based on certain variables. I am currently facing an issue trying to insert that link into <a href="Some Link"> Some Text </a> Both the "Some Text" and "Som ...

What is the reason for the removal of the `?` decorator in this mapped type? Are there alternative methods to achieve a similar outcome without eliminating it

Challenge In the process of creating a mapped type that excludes properties of type Function, we encountered an issue. Our current method not only eliminates functions but also strips away the optional decorator (?) from the mapped properties. Scenario ...

Firebase and Nx: Encountering issues with running emulators

I've been attempting to launch the Firebase emulators within the Nx workspace. Initially, I added firebase to my project: npm install firebase @angular/fire --save nx g @angular/fire:ng-add // configures the package in the project (unsuccessful) ng ...

Utilize multiple validators.patterns within a single value for enhanced data validation

I need to implement two different patterns for the formControlName='value' based on the type selected. If type is 'A', I want to use the valuePattern, and if type is 'B', I want to use the uname pattern. This is my HTML code: ...

npm ERROR! Encountered an unexpected symbol < within the JSON data at position 12842

I keep encountering an error every time I attempt to install a package or run npm install. Despite my limited experience with Angular 4, having only started using it a week ago, I am puzzled by this error. Any guidance on how to resolve it would be greatly ...

How to simulate loadStripe behavior with Cypress stub?

I am struggling to correctly stub out Stripe from my tests CartCheckoutButton.ts import React from 'react' import { loadStripe } from '@stripe/stripe-js' import useCart from '~/state/CartContext' import styles from '. ...

Is there a way to detect and handle errors triggered by a callback function?

My component has the following code snippet: this.loginService.login(this.user, () => { this.router.navigateByUrl('/'); }); Additionally, my service contains this method: login(credentials, callback) { co ...