我有一个Codeigner框架投票应用程序,我想在主页中添加一个新功能,每X秒更新一次主页,我已经准备好了SQL查询,以返回带有项目ID的项目投票,Codeigner将为其提供JSON格式。
前端我正在使用angularjs,每X秒自动获取JSON内容的最佳方法是什么。
我会使用$interval和$http函数,因为您使用的是angular。
它看起来像这样:
// 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);
};