2016-12-25 69 views
0

創建互聯網商店,當我管理用戶購物車頁面在調用deleteFromCart()後Angular不刷新視圖,所以我需要刷新頁面。我錯過了什麼?由於我做了。然後承諾(F()),但它不工作 JS代碼:Angular.then()不工作(發送獲取請求後不刷新視圖)

var url = $location.absUrl() + "/products"; 

getProducts(); 
function getProducts() { 
    $http.get(url).then(function (response) { 
     $scope.total = 0; 
     $scope.products = response.data; 
     $scope.products.forEach(function (product) { 
      $scope.total = $scope.total + product.price; 
     }) 
    }); 
} 

$scope.deleteFromCart = function (productId) { 
    $http.get(url + '/' + productId).then(function() { 
     getProducts(); 
    }); 
} 

查看:

<div class="panel panel-primary" ng-controller="CartCtrl"> 
    <div class="panel-heading"> 
     <h3 class="panel-title">Your Cart</h3> 
    </div> 
    <div class="panel-body "> 
     <table class="table table-bordered"> 
      <thead> 
      <tr> 
       <th>Product name</th> 
       <th>Price</th> 
       <th>Quantity</th> 
       <th class="text-center">Action</th> 
      </tr> 
      </thead> 
      <tr ng-repeat="product in products"> 
       <td>{{product.title}}</td> 
       <td>{{product.price}}$</td> 
       <td>3</td> 
       <td class="text-center"> 
        <a href="#" ng-click="deleteFromCart(product.id)" class="btn btn-danger btn-xs">Remove</a> 
       </td> 
      </tr> 



     </table> 
     <h4><span class="label label-primary pull-left">TOTAL : {{total}}$</span></h4> 
     <button class="btn btn-success pull-right">Confirm Order</button> 
    </div> 

</div> 

回答

1

要刷新頁面只是從刪除產品爲什麼清單

<td class="text-center"> 
    <a href="#" ng-click="deleteFromCart(product.id,$index)" class="btn btn-danger btn-xs">Remove</a> 
    </td> 

$scope.deleteFromCart = function (productId,index) { 
    $http.get(url + '/' + productId).then(function() { 
     $scope.products.splice(index,1); 
    }); 
} 
+0

你在客戶端刪除它,不知道該對象是否在服務器上被刪除,這是一個很好的做法嗎? – qwd412

+0

從服務器端獲得該項目被刪除後,您可以從客戶端刪除。然後功能對應該項目被刪除成功。 –

+0

我認爲這太複雜了,是否有任何其他解決方案,以便我發送刪除請求和刷新產品數組,我想解決方案非常簡單,但我錯過了一個細節,哪一個? – qwd412