2014-11-01 60 views
0

我有一個Codeigniter框架投票應用程序,我想在主頁中添加一個新功能,每X秒更新一次主頁,我已經準備好了SQL查詢以返回物品ID, Codeigniter將爲其提供JSON格式。每X秒獲取Codeigniter內容

前端我正在使用angularjs,每X秒自動獲取JSON內容的最佳方法是什麼?

+1

$超時/ $間隔調用服務器。 – Shomz 2014-11-01 22:38:12

回答

2

我會使用$ interval和$ http函數,因爲您使用的是角度。

這將是這個樣子:

// Set the time (in milliseconds) 
var time = 1000; 
// Call this in your controller 
function($scope, $interval, $http) { 
    // Start the interval 
    $interval(function() { 
    // Send a GET request to your URL 
    $http.get('/my/url', { params: {} }).success(function(data) { 
     // Add the new JSON data to current scope 
     $scope.json_content = data.json_content; 
    }); 
    }, time); 
};