0
我正在購買購物車應用程序。將產品添加到購物車後,我允許用戶更改產品數量,這將影響產品總價格和產品總數。我也有一個輸入優惠券代碼的文本字段,並在總計上申請折扣。之後,我將重定向到訂單摘要頁面。如果我使用瀏覽器後退按鈕返回到購物車列表頁面,那麼我在該頁面中做的更改(如增加/減少數量,添加折扣優惠券代碼等)不會顯示,而是顯示價格當我第一次以初始數量和價格重定向到購物車清單頁面時。在瀏覽器上返回以前的值重置
我在下面分享我的代碼。請在我使用瀏覽器返回按鈕返回到此頁面時,幫助我理解阻止修改的方法。
價格計算在該頁面的控制器全部完成,價格更新是一個觀察者內部完成(這是一個原因,我沒能忍住新修改)。
controller.js
// to get the price update when the cart list page is loaded from product list/details page
if($rootScope.cart.length > 0){
for (var i = 0; i < $rootScope.cart.length; i++) {
$rootScope.products[i].p_price = $rootScope.products[i].p_quantity * $rootScope.products[i].p_originalprice;
$rootScope.arrGrandTotal.push($rootScope.products[i].p_price);
$scope.grandTotal += $rootScope.arrGrandTotal[i];
}
//$scope.totalUpdate = $scope.grandTotal;
//console.log("$rootScope.grandTotal :"+$scope.grandTotal);
}
// this function is fired when product quantity text field is updated with new product quantity
$scope.updateJson = function($index){
$rootScope.cart[$index].p_price = $rootScope.cart[$index].p_quantity * $rootScope.cart[$index].p_originalprice;
}
// watching the array inside which the products added in cart are pushed. From this array I am getting the product price
$scope.$watch('cart', function(newValue, oldValue){
for(var i=0; i<$rootScope.cart.length; i++){
if(newValue[i].p_price != oldValue[i].p_price){
if(newValue[i].p_price > oldValue[i].p_price){
$scope.grandTotal += (newValue[i].p_price - oldValue[i].p_price);
} else {
$scope.grandTotal -= (oldValue[i].p_price - newValue[i].p_price);
}
}
}
}, true);
view.html
<input type="text" class="text-center form-control input-sm" ng-model="eachAddedProd.p_quantity" ng-change="updateJson($index)">
<div class="row">
<div class="text-center">
<div class="col-sm-9">
<h6 class="text-right">Do you have a coupon code? if not go <a href="https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=coupon+code" target="_blank">here</a></h6>
</div>
<div class="col-sm-3">
<div class="col-sm-6">
<input class="form-control coupon-entry" type="text" name="" value="" maxlength="9" ng-model="coupon">
</div>
<div class="col-sm-6">
<div class="row"><button type="button" class="btn btn-default btn-block" ng-click="couponCode()">Apply Code</button></div>
</div>
</div>
</div>
</div>
<div class="panel-footer">
<div class="row text-center">
<div class="col-sm-10">
<h4 class="text-right">Total <strong ng-class="{'old-price' : strikePrice}">{{grandTotal | currency}}</strong> <strong ng-hide="discountedPrice">{{discountPrice | currency}}</strong></h4>
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-success btn-block" ng-click="gotoInvoice()">
Checkout
</button>
</div>
</div>
</div>
讓我知道,如果我與我的問題清楚,否則你將無法找到解決這個問題。
在此先感謝。
您可以將自己的價值保存在Cookie或本地存儲中。 –
我已經嘗試過這個過程,但由於價格更新發生在觀察者內部,所以新的價格只在觀察者工作時才起作用。 – NoviceCoder