I am getting wrong value, because of multiple change deduction. how to prevent?
app.component.ts :
import { Component, OnInit } from "@angular/core";
interface PropsData {
productName: string;
value: number;
count: number;
details: (value1: number, value2: number) => number;
}
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
products: PropsData[];
allTotal: number = 0;
ngOnInit() {
this.products = [
{
productName: "sugar",
value: 20,
count: 1,
details: (value1, value2) => {
return this.grandTotal(value1, value2);
}
},
{
productName: "salt",
value: 40,
count: 1,
details: (value1, value2) => {
return this.grandTotal(value1, value2);
}
},
{
productName: "jackery",
value: 70,
count: 1,
details: (value1, value2) => {
return this.grandTotal(value1, value2);
}
}
];
}
updateCount(product: PropsData) {
product.count++;
}
updateAllTotal(itemTotal: number): void {
this.allTotal = this.allTotal + itemTotal;
}
grandTotal(value1, value2): number {
const total = value1 * value2;
this.updateAllTotal(total);
return total;
}
}
html:
<div>
<div *ngFor="let product of products">
<h1>{{product.productName}}</h1>
<p>{{product.count}}</p>
<p>Total: {{product.details(product.count, product.value)}}</p>
<p><button (click)="updateCount(product)">Add Product</button></p>
</div>
<h2>Grand Total: {{allTotal}}</h2>
</div>