2016-02-19 107 views
0

我撥打了一個獲取電話並檢索了一些我想用來撥打另一個撥打電話的數據,並將該數據放入$scope中。像這樣的東西。

$http.get('api/url/items').then(function(res){ 
    $scope.items = res.data; 
    $http.get('api/url/names/' + res.data[0].id).then(function(res){ 
    console.log(res.data.name); 
    $scope.name = res.data.name; 
    } 
} 

$scope.items被填充,但$scope.name不是。 console.log提供了正確的數據,但似乎在渲染後的視圖之後。在呈現視圖之前,如何獲得第二個$http.get以在$scope之間填充?

回答

1

轉換這個

$http.get('api/url/names/res.data[0].id') 

這個

$http.get('api/url/names/'+res.data[0].id) 
+0

對不起,這是一個錯字,你的迴應是我的代碼。 –

0

我在我的原代碼,是不是在說我現在解決了我的問題,簡單的兩個問題。

  1. 我正在使用一個函數來調用第二個$ http.get。到那個函數回調的時候,原來的$ http.get已經解決了,所以我在第一個$ http.get中移動了這個函數代碼。
  2. 我正在使用for循環(var i)來調用第二個$ http.get函數並在我的$ scope.items [i]中設置值。當$ http.get解決了我var已經達到它的極限,導致一個未定義的錯誤。我創建了一個計數器變量,僅在第二個$ http.get中更改,以便索引正確匹配。