I am currently working on a budgeting application that incorporates an array of expenses with a price property. Each expense is defined within an Expense model in Typescript. While I can easily access the price property using ngFor loop in HTML, I'm curious if it's possible to achieve the same using a for loop directly in Typescript.
expenses.service.ts
import { Injectable } from '@angular/core';
import { Expense } from '../expenses-list/expense/expense.model';
@Injectable({
providedIn: 'root'
})
export class ExpensesService {
expenses: Expense [] = [
new Expense('car payment', 350) // price
];
constructor() { }
onAddExpenseToExpenses(expense: Expense) {
this.expenses.push(expense);
}
// EXAMPLE //
onCalculate() {
// get prices from listed items
// add all the prices
// subtract income from sum of prices
}
Apologies if my explanation is not very clear, as I am relatively new to Angular 6.
Thank you for your assistance! =)
Below is the structure for my expense model:
export class Expense {
private item: string;
private price: number;
constructor(item: string, price: number) {
this.item = item;
this.price = price;
}
}