2017-03-01 55 views
0

我有$scope.participants = []作爲全局變量。我的問題是,當我調用下面的函數後,在另一個函數中使用此變量。 $scope.participants是空的。但是,這不是當我檢查response.data

$scope.getParticipants = function(seminar_id,seminar_name,seminar_code) { 
    $http({ 
     method: 'GET', 
     dataType: 'JSON', 
     params: { 'seminar_id' : seminar_id }, 
     url: 'getParticipants' 
     }).then(function(response) { 
      $scope.participants=response.data; 
     }); 
    } 
+0

您是否看到控制檯中的任何錯誤 – Sajeetharan

+0

'$ http'是異步調用。在$ $ $返回數據之前'$ scope.participants'爲空。 –

+0

檢查這是否可以解決您的問題。 http://stackoverflow.com/questions/18421830/how-to-wait-till-the-response-comes-from-the-http-request-in-angularjs –

回答

0

你可以看看這個工作plnkr,你需要等到你的承諾解決。

$scope.getParticipants= function(seminar_id,seminar_name,seminar_code){ 
$http({ 
    method: 'GET', 
    dataType: 'JSON', 
    params: { 'seminar_id' : seminar_id }, 
    url: 'getParticipants' 
    }).then(function(response) { 
     $scope.participants=response.data; 
     $scope.callBack(); 
     console.log(response.data); 
    }); 
} 
$scope.getParticipants('a','b','c'); 
+0

沒有什麼可以在1秒內完成的 –

+0

我也試過了,它不會在1秒內完成。 – dan

+0

添加回調函數以在異步調用完成時通知範圍內的某個函數調用。檢查更新[plnkr](https://plnkr.co/edit/ATlhJvXgW3K0JVMqQCJm?p=preview) –