아마도이 질문은 이미 물어 보았습니다 만, 아무런 성공도없이 내 오후의 대부분을 검색하고 시도했기 때문에 누군가가 나를 도와 줄 수 있기를 바랍니다.각도 : 몇 초마다 데이터 업데이트
몇 초마다 공장 서비스에서 설정 한 $ http.get() 데이터를 업데이트하고 싶습니다.
나는 내 코드에 몇 가지 의견을 추가했으며 내가 뭘 시도했는지 볼 수있는 몇 가지 오래된 것들을 남겼다. 에,
ovwid.factory('recentClients', [
'$http',
'$rootScope',
function ($http, $rootScope) {
var apiURL = '/plugins/data/get_client.php';
var promise;
var recentClients =
{
async: function()
{
if (!promise)
{
// $http returns a promise, which has a 'then' function, which also returns a promise
promise =
$http.get(apiURL)
.then(function (response) {
// The then function here is an opportunity to modify the response
// The return value gets picked up by the then in the controller.
return response.data;
});
}
// Return a promise to the controller
return promise;
}
}
return recentClients;
}]);
ovwid.controller(‘client’Ctrl, [
'$scope',
'recentClients',
'$interval',
function ($scope, recentClients, $interval) {
$scope.loading = true;
function reloadData() {
// a call to the async method
recentClients().async().then(function(data) {
// console.log(data);
$scope.loading = false;
$scope.client = data;
});
}
// Initizialize function
reloadData();
// Start Interval
var timerData =
$interval(function() {
reloadData();
}, 1000);
// function myIntervalFunction() {
// var cancelRefresh = $timeout(function myFunction() {
// reloadData();
// console.log('data refres')
// cancelRefresh = $timeout(myFunction, 5000);
// },5000);
// };
// myIntervalFunction();
// $scope.$on('$destroy', function(e) {
// $timeout.cancel(cancelRefresh);
// });
}]); // [/controller]
답장을 보내 주셔서 감사합니다. 'recentClients.async()를 호출하고 있습니다. –
질문 : – charlietfl
당신의 관찰 덕분에'! promise'를 제거하여 해결했습니다 –