2017-08-16 20 views
0

我通過函數動態使用數據加載,然後使用ng-repeat填充數據。ng-repeat顯示的數據不存在

下面是控制器代碼。

$scope.loadproducts = function(item_id) { 

     var pdata = $.param({ 
      item_id : item_id, 
     }); 

     $http({ 
      method: 'POST', 
      url: baseurl + 'api/get_items_in_category_cart', 
      data: pdata, 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     }).then(function successCallback(response) { 
      $scope.items = response.data.products; 
      $scope.description = response.data.description; 
      console.log($scope.items); 
      $timeout(apply_tooltip, 500); 
     }, function errorCallback(response) { 

     }); 
    }; 

查看

<div class="container col-md-6" ng-repeat="item in items"> 
    <div class="panel panel-primary"> 
     <div class="panel-heading"> 
      <h4 class="panel-title">{{ item.title }}</h4> 
     </div> 
     <div class="panel-body"> 
      <div class="col-md-3 col-xs-5"><a href="#" class="thumbnail"><img ng-src="{{ item.thumbnail }}"></a></div> 
      <div class="col-md-9" style=""> 
       <div class="row"> 
        <div class="col-md-12"> 
         <span style="font-size: 14px; font-weight: 500;">{{ item.description }}</p> 
        </div> 
       </div> 
       <div class="row"> 
        <div class="col-md-4 text-right" style=" text-align: left;"> 
         <p>{{item.price | currency:"NZ $"}}</p> 
        </div> 
        <div class="col-md-8" style=" text-align: left;"> 

         <input type="text" value="{{item.qty}}" style="width:50px; height: 28px; line-height: 28px;" readonly id="qty_{{item.id}}"/> 

        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

在初始頁面加載的代碼做的事情是應該做的。 有一個用戶進入另一個頁面的繼續按鈕。 如果用戶返回,這些項目應該重新加載。

問題是在重新加載期間應該顯示每個項目數量的文本框 - >> value =「{{item.qty}}」似乎顯示所有qty文本框的相同數量,而不管他們原來的數量。

console.log($ scope.items)顯示它應該具有的正確值。這只是顯示錯誤的數量。

任何想法的傢伙?

+0

你的apply_tooltip是做什麼的? –

+0

顯示懸停時的工具提示 –

+0

您可以嘗試換行$ scope.items = response.data.products; $ scope.description = response.data.description;還有一個$ timeout(0)呢? –

回答

0

如果你的數據是正確加載,你可以有兩種方式:

1 - 嘗試使用NG-模型在輸入

<input type="text" ng-model="item.qty" style="width:50px; height: 28px; line-height: 28px;" readonly id="qty_{{item.id}}"/> 

您可以去掉「只讀'屬性並添加一個ng-disabled =「true」。

2 - 您可以在獲取新值之前嘗試清除您的列表。因此,在您從後端加載您的值之前,您可以清除您的數據:

$scope.items = []; 

並在請求完成時再次放入數據。

請注意,這是第二個解決方案,它不是傳統的,應該使用,如果你不能找到另一種解決方案。