I am trying to get total price of the orders made that I have stored in an array. The code looks something like this.
import { Component } from '@angular/core';
import { Config } from '../config';
var ordersArray = [
];
@Component({
selector: 'orders-page',
templateUrl : 'orders.html'
})
export class Orders{
public orders = ordersArray;
public config = new Config;
public addOrder(item){
ordersArray.push(item);
}
public clearOrders(){
var length = this.orders.length;
for(let i = 0; i < length; i++){
ordersArray.pop();
}
}
public total = this.getPrice();
public getPrice(){
let price = 0;
for(let order of ordersArray){
price = (parseInt(order.item.price) * parseInt(order.quantity) ) + price;
}
return price;
}
public clearOrder(order){
for(let i = 0;i < this.orders.length;i++){
if(order.title == this.orders[i].title){
ordersArray.splice(i,1);
}
}
}
}
And the template, I have {{total}}
. Now, when I remove an element from the ordersArray
, I want the {{total}}
to automatically update. How do I do that ?
Call getPrice()
after updating the ordersArray
, So call getPrice()
inside the clearOrder()
Try this
public clearOrder(order){
for(let i = 0;i < this.orders.length;i++){
if(order.title == this.orders[i].title){
ordersArray.splice(i,1);
}
}
this.total = this.getPrice();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments