我使用的可以在這裏找到了ngStorage模塊:https://github.com/gsklee/ngStorage
我設置一個lastSeenId在localStorage的爲實時更新社交飼料,因爲這是一個混合的應用程序(Laravel/AngularJS)我無法將其存儲在$scope
中。
我StatusesController
得到一個數組或數據(或什麼,如果沒有查看)以往X秒,我得到的數組的最後一個項目,並設置我的$localStorage
VAR是這樣的: $localStorage.lastStatusSeen = lastId;
我的導航控制器在這裏我爲用戶設置了一些新的計數器,以查看他們是否錯過了任何東西,我得到的lastSeenId是這樣的: $scope.$storage.lastStatusSeen
它被髮送到我的API返回一些未看到的狀態帖子。
這一切都有效,用戶在狀態頁面上,如果新數據,然後將本地存儲var設置爲數據數組中的最後一個ID。
問題是我的NavigationController
未找到此更改並始終通過用戶第一次訪問該頁面時傳遞的lastSeenId
。
有沒有辦法在本地存儲中查看該密鑰以進行更改?
編輯:按照要求,更新的代碼
app.controllers.navigationController = ['$scope', 'pollingService', '$localStorage', function($scope, pollingService, $localStorage) {
$scope.lastStatusSeen = $localStorage.lastStatusSeen;
$scope.$watch(function() {
return $localStorage.lastStatusSeen;
}, function (newVal, oldVal) {
if (!newVal) {
return;
}
if (newVal !== oldVal) {
// Assign to a local scoped var, or anything, here...
$scope.lastStatusSeen = newVal;
// I suggest call a method to do your logic outside this
//$scope.onIdChange(newVal, oldVal); // If you need oldVal
}
});
pollingService.startPolling('emailCount', 'api/email/new-count', 5000, function(response) {
$scope.emailCount = response.data;
});
pollingService.startPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen, 5000, function(response) {
$scope.socialCount = response.data;
});
}];
這似乎並不工作,我做如上代碼提示,我將其分配給一個範圍var當我的AJAX調用仍顯示ID 23,當本地存儲顯示24? – ChrisBratherton
問題已更新,我將其放置在我的導航控制器中? – ChrisBratherton
但是,如果我在監視器中放入pollingService調用,只會在ID更改後才調用該調用?該功能每5秒左右輪詢一次服務器? – ChrisBratherton