我的发票有问题。
在一张发票中,我可以添加更多产品。产品分为5个类别,每个类别都有默认价格。例如
Product 1 : TV, default price 350 $
Product 2 : Books, default price 50 $
.......
Product 5 : Phone, default price 150 $
提交发票时,我想计算总价,例如:
1. TV 1 * 350$ = 350$
2. Angular Book 1 * 50$ = 50$
....
5. Samsung Phone 2 * 150$ = 300$
为此,我编写以下代码:
组件ts。
onAddProduct() {
let products = [];
let newProduct = this.addProductForm.value
newProduct.p_Subtotal = this.producte1.default_price1 * this.p_Quantity;
newProduct.p_Subtotal = this.producte4.default_price4 * this.p_Quantity;
newProduct.p_Subtotal = this.producte2.default_price2 * this.p_Quantity;
newProduct.p_Subtotal = this.producte3.default_price3 * this.p_Quantity;
newProduct.p_Subtotal = this.producte5.default_price5 * this.p_Quantity;
let prod = new Products(newProduct)
this.product = newProduct;
this.ps.addProduct(this.product);
this.router.navigate(['/invoice']);
}
在这段代码中,问题始终存在:p_Quantity * default_price
,但在控制台中显示正确的值。
您能建议任何解决方法吗?
解决方案
onAddProduct() {
let products = [];
let newProduct = this.addProductForm.value
if (this.addProductForm.controls.p_Product_type_id.value === '2') {
newProduct.p_Subtotal = this.producte2.default_price2 * this.p_Quantity;
let prod2 = new Products(newProduct);
} else if (this.addProductForm.controls.p_Product_type_id.value === '1') {
newProduct.p_Subtotal = this.producte1.default_price1 * this.p_Quantity;
let prod1 = new Products(newProduct);
} else if (this.addProductForm.controls.p_Product_type_id.value === '3') {
newProduct.p_Subtotal = this.producte3.default_price3 * this.p_Quantity;
let prod3 = new Products(newProduct);
} else if (this.addProductForm.controls.p_Product_type_id.value === '5') {
newProduct.p_Subtotal = this.producte5.default_price5 * this.p_Quantity;
let prod5 = new Products(newProduct);
} else if (this.addProductForm.controls.p_Product_type_id.value === '4') {
newProduct.p_Subtotal = this.producte4.default_price4 * this.p_Quantity;
let prod4 = new Products(newProduct);
}
this.product = newProduct;
this.ps.addProduct(newProduct);
this.router.navigate(['/invoice']);
}