我正在嘗試在Angular中編寫一個應用程序,每10秒更新一次數據。我的問題是,每次刷新我的頁面時,函數只會被調用一次。
console.log只是爲了確保該函數只被調用一次。
這是我發起的間隔碼:
var app = angular.module('myApp',['ngRoute', 'ui.bootstrap', 'ngNotify', 'ngTagsInput', 'djds4rce.angular-socialshare']);
app.run(function($FB, $http, $rootScope, $interval){
$FB.init('527807327395199');
$interval(function() {callAtInterval($http, $rootScope)}, 1000, true);
$rootScope.count = 0;
});
function callAtInterval($http, $rootScope) {
$http.get("/points-for-school")
.then(function(response){
$rootScope.schoolCompetitionPoints = response.data;
console.log($rootScope.count++);
console.log(response);
}, function(response){
console.error(response);
});
}
的是,我這個在app.run方法的問題?
我必須將代碼放入控制器才能正常工作嗎?
如果有一種方法可以在不創建控制器的情況下完成這項工作,我更喜歡它。
是的,這是真的。我相應地根據你的建議改變了代碼,但它仍然只被調用一次。 – kitkong
你需要添加count參數,就像我在我的答案中寫的一樣。感謝您的幫助,但它幫助我解決了第一個問題,就是我如何通過該功能。 – kitkong