I have the following Product object inside an Angular application. The product contains array of orders and other product information.
Product.ts
export interface Product {
ProductID: number;
ProductName: string;
Orders: Order[]
}
I need to display product information and generate order summary inside a child angular component.
order-summary.component.ts
export class OrderSummary {
_product: Product;
@Input() set product(value: Product) {
this._product = value;
this.generateOrderSummary(value.Orders);
}
private generateOrderSummary(orders: Order[]) {
// Logic to generate order summary
}
}
order-summary.component.html
<b>{{_product.ProductName}}</b>
<b> ... show other product details </b>
<table> ... show generated order summary ... </table>
I believe every time the product object changes, it will generate the order summary (generateOrderSummary) even if the Orders array hasn't been changed. What is a better way to fix this issue?
I am thinking of adding another property for Orders[] so I can regenerate summary only when order has been changed. However, product already contains orders not sure if it is a good design.
order-summary.component.ts
export class OrderSummary {
_product: Product;
@Input() set product(value: Product) {
this._product = value;
}
@Input() set orders(value: Orders[]) {
this.generateOrderSummary(value.Orders);
}
private generateOrderSummary(orders: Order[]) {
// Logic to generate order summary
}
}
Are there any better solutions?