2017-08-03 59 views
0

我試圖從我的控制器中使用HTTP獲取請求從laravel API檢索響應,並通過ng-repeat循環查看視圖中的所有數據,在控制檯中響應但不在視圖中顯示任何內容。 這是我的JSON對象:使用ng-repeat從對象的http響應數組中檢索數據

[ 
{ 
    "id":1, 
    "product_id":"ABCly119", 
    "name":"Shirt", 
    "category_id":"fashion", 
    "color":"Grey", 
    "size":"L", 
    "brand":"Tims", 
    "type":"Polo", 
    "price":11000, 
    "main_image":null, 
    "vat":null, 
    "description":"A very good shirt", 
    "product_condition":"new", 
    "created_at":"2017-07-31 14:36:17", 
    "updated_at":"2017-07-31 15:37:21"}, 
{ 
    "id":2, 
    "product_id":"ABCly139", 
    "name":"Shirt", 
    "category_id":"fashion", 
    "color":"Black", 
    "size":"M", 
    "brand":"Tims", 
    "type":"Polo", 
    "price":10000, 
    "main_image":null, 
    "vat":null, 
    "description":"A very good shirt", 
    "product_condition":"new", 
    "created_at":"2017-07-31 14:39:47", 
    "updated_at":"2017-07-31 14:39:47"}, 
{ 
    "id":3, 
    "product_id":"ABCly139", 
    "name":"Shirt", 
    "category_id":"fashion", 
    "color":"Blue","size":"S", 
    "brand":"Tims", 
    "type":"Polo", 
    "price":12000, 
    "main_image":null, 
    "vat":null, 
    "description":"A very good shirt", 
    "product_condition":"new", 
    "created_at":"2017-07-31 14:41:06", 
    "updated_at":"2017-07-31 14:41:06" 
    } 
] 

這是我的控制器

angular 
.module('productsController',[]) 
.constant('baseUrl', 'http://127.0.0.1:8000/api/product') 
.controller('productCtrl',['$scope','$log','$http','baseUrl','$rootScope',function($scope,$log,$http,baseUrl,$rootScope){ 
    $log.info('Product controller loaded'); 

    $http.get(baseUrl + '/fetch') 
     .then(function (response){ 
      $scope.response = response; 

      var i = $scope.response; 

      angular.forEach(i, function(item){ 
       $scope.product = item; 
       console.log($scope.product);  
      }); 


      return response.data; 
     },function (error){ 
      $scope.error = error; 
      $log.info($scope.error); 
     }); 
}]); 

這是我的視圖

<ul> 
    <li ng-repeat="x in products"> 
     <span>{{x[0].name}}</span> 
    </li>      
</ul> 

我嘗試使用上面的代碼它記錄到控制檯,但不顯示上視圖

+1

您應該爲您的後端調用使用'service',而不是直接在您的組件中使用它。 –

回答

0

$ scope.product =項目將覆蓋所有項目,除了我數組的最後一個項目,您做的更好下面

$scope.products=[]; 
    angular.forEach(i, function(item){ 
     $scope.products.push(item); 
    }); 

<ul> 
    <li ng-repeat="x in products"> 
     <span>{{x.name}}</span> 
    </li>      
</ul> 

或低於標記打印這種方式name屬性

的所有值
<ul> 
    <li ng-repeat="x in response.data"> 
     <span>{{x.name}}</span> 
    </li>      
</ul> 
+0

嘗試過,但還沒有工作 – Oyinkan

+0

今天早上再次嘗試它工作!!!!。非常感謝 – Oyinkan

+0

@Oyinkan,很高興工作,快樂編碼 – Praveen

0

有沒有$ scope.products,只需$ scope.product,試試

<ul> 
    <li ng-repeat="x in response"> 
     <span>{{x[0].name}}</span> 
    </li>      
</ul> 
+0

我不認爲數組中的'$ scope.response'中的每個項目,所以你應該擺脫第一個數組索引,只需要:{{x.name}}' –

+0

就像問題所示,響應IS和數組,你可以迭代。 – flaalf

+0

我很清楚'$ scope.response'是一個數組,但是你的代碼表明該數組中的每一項都是一個數組本身。 –

0

控制器更改爲:

angular 
.module('productsController',[]) 
.constant('baseUrl', 'http://127.0.0.1:8000/api/product') 
.controller('productCtrl',['$scope','$log','$http','baseUrl','$rootScope',function($scope,$log,$http,baseUrl,$rootScope){ 
    $log.info('Product controller loaded'); 

    $http.get(baseUrl + '/fetch') 
     .then(function (response){ 
      $scope.products = response 
     },function (error){ 
      $log.info(error); 
     }); 
}]); 

和你的看法:

<ul> 
    <li ng-repeat="x in products"> 
     <span>{{x.name}}</span> 
    </li>      
</ul> 

你應該很好去。

+0

抱歉,但它沒有奏效。我也早些時候嘗試過。 – Oyinkan

+0

如果您的響應對象沒有數據成員,它似乎有點奇怪。您不需要遍歷數組並將元素推送到另一個數組。我要編輯這個答案來反映這一點。 –