0
我有一個小問題,我無法在每次輪詢超時時更新視圖中的Poller.msgdata。
所以我想要做的是從服務接收Poller.msgdata和Poller.newdata TRUE或FALSE條件,然後在{{inbox}}內動態輸出。然而,$ scope.inbox自第一次檢索數據是未定義的時候就是空的,這不是問題。它的事實是msgdata和newdata在Poller超時時不會更新。
{{inbox}}每次都應該爲TRUE或FALSE和UPDATE。 如果你需要更多的細節,請讓我知道。
app.controller("taskbarController", ['$scope', 'authData', '$location', 'projectsModal', 'sendMessageModal', 'Poller',
function ($scope, authData, $location, projectsModal, sendMessageModal, Poller) {
$scope.inbox = Poller.msgdata;
$scope.project = Poller.newdata;
$scope.projects = Poller.projects;
$scope.messages = Poller.messages;
}]);
這裏是更新MSGDATA和newdata的輪詢:
app.factory('Poller', Poller);
Poller.$inject = ['$http', '$timeout'];
function Poller($http, $timeout) {
var projectcache = { response: [], calls: 0 };
var msgcache = { response: [], calls: 0 };
var newdata;
var msgdata;
var poller = function() {
$timeout(poller, 5000);
$http.get('http://localhost/app/controllers/php/getProjects.php')
.then(function(r) {
if (r.data.projects.length > projectcache.response.length) {
newdata = true;
projectcolor = 'green';
angular.copy(r.data.projects, projectcache.response);
} else {
newdata = false;
projectcolor = 'green';
};
});
$http.get('http://localhost/app/controllers/php/getMessages.php')
.then(function(m) {
if (m.data.messages.length > msgcache.response.length) {
msgdata = true;
msgcolor = 'green';
angular.copy(m.data.messages, msgcache.response);
} else {
msgdata = false;
msgcolor = 'green';
};
});
};
poller();
return {
projects: projectcache.response,
messages: msgcache.response,
newdata: newdata,
msgdata: msgdata
};
};
最後我顯示部分內部爲:
{{inbox}}
在控制檯上的任何錯誤? –
@ A.J沒有錯誤我的朋友,因爲沒有什麼語法錯誤。我試圖找出如何在Poller更新其值時更新2 $ scope項目? – Elevant
也許嘗試在你的任務欄控制器中設置'$ timeout(function() {scope。$ apply(); },5000)',以確保範圍正在意識到他正在獲取數據? 否則請嘗試$ interval並每秒記錄您的變量以檢查內容是否更改。 – graphefruit