2017-04-26 6 views
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> 

は他のあなたは、この問題に対する解決策を見つけることができません、私は私の問題では明らかだなら、私に教えてください。

ありがとうございます。

+0

クッキーまたはローカルストレージにあなたの価値を保持することができます。 –

+0

私はこのプロセスを試しましたが、ウォッチャー内で価格の更新が行われているため、ウォッチャーが動作しているときにのみ新しい価格が機能していません。 – NoviceCoder

答えて

0

クッキーまたはローカルストレージに値を保持することができます。

関連する問題