我正在使用Angular2开发电子商务Web应用程序。
我已在会话中保存了有序信息。
addToCart(productId:string,noOfItems:number):void{
let itemCounts;
let selectedItems=[];
if(sessionStorage.hasOwnProperty("cartItemCount"))
{
itemCounts = parseInt(sessionStorage.getItem("cartItemCount"));
itemCounts++;
sessionStorage.setItem("cartItemCount",itemCounts.toLocaleString());
}else{
sessionStorage.setItem("cartItemCount","1")
}
this._addtocartService.updateCount.emit("itemAdded");
this._notificationService.success("Success","item added to Cart");
let item={
productId:productId,
productName:this.productDetail.name,
packageName:this.selectedPackage.packageName,
Rate:this.selectedPackage.price,
unit:this.selectedPackage.unitPerQuantity,
quantity:noOfItems,
packageId:this.selectedPackage._id,
owner:this.owner._id
}
if(sessionStorage.getItem("CartContent") != undefined)
{
let test = JSON.parse(sessionStorage.getItem("CartContent"));
for(let loop=0;loop<test.length;loop++) {
if(test[loop].productId==productId) {
}
}
}
if(sessionStorage.hasOwnProperty("CartContent")) {
selectedItems=JSON.parse(sessionStorage.getItem("CartContent"));
}
selectedItems.push(item);
sessionStorage.setItem("CartContent",JSON.stringify(selectedItems));
console.log(sessionStorage.getItem("CartContent"));
}
我有cartItem组件,其中显示所有有序信息。在那个组件中,我从这样的会话中获得订购商品。
export class cartItemComponent{
private Orders:any;
private total:number;
constructor(private dataService:DataService,
private loggerService:LoggerService){
this.getCartContent();
this.calculatePrice();
}
getCartContent(){
this.Orders = JSON.parse(sessionStorage.getItem("CartContent"));
console.log("from Orders");
console.log(this.Orders);
var products = JSON.parse(sessionStorage.getItem("product"));
}
calculatePrice() {
let i;
for (i = 0; i < this.Orders.length; i++) {
console.log("this is order");
console.log(this.Orders[i]);
this.dataService.post("order/prices", this.Orders[i])
.subscribe(res => {
console.log(res);
console.log(this.Orders[i]);
this.total+=res.totalPrice;
this.Orders[i]['totalPrice'] = res.totalPrice;
});
console.log("Total is " + this.total);
if(i==this.Orders.length){
this.loggerService.log(this.Orders,"green");
}
// setTimeout(()=>{
// for (i=0;i<this.Orders.length;i++){
// this.Orders[i]['totalPrice']=233;
// }
//
// console.log("inside timeout");
// },3000);
}
}
//}
}
我将这些信息从calculatePrice()方法传递给api,只得到api中每种类型项目的总价。 但在订阅内,this.Orders [i]未定义,我无法添加新的json密钥this.Orders [i] [&#39; totalPrice&#39;]及其内部的值。
接下来,我必须使用循环语句在表中显示该信息。
帮助将不胜感激
答案 0 :(得分:1)
我认为你在数据服务中传递订单的方式就是创造问题。您正试图在订阅中访问i
的值,并且订阅不同步工作。所以尝试在另一个变量中获取当前Order的引用,然后将其传递给dataService,如下所示。
calculatePrice() {
let i;
for (i = 0; i < this.Orders.length; i++) {
console.log("this is order");
console.log(this.Orders[i]);
let currOrder = this.Orders[i]; //get current order in a variable
this.dataService.post("order/prices", currOrder)
.subscribe(res => {
console.log(res);
console.log(currOrder);
this.total+=res.totalPrice;
this.currOrder['totalPrice'] = res.totalPrice;
});
console.log("Total is " + this.total);
if(i==this.Orders.length){
this.loggerService.log(this.Orders,"green");
}
}
}