Javascript 查看$localStorage的更改
我使用的是NGS存储模块,可在此处找到: 我正在localStorage中为实时更新社交提要设置lastSeenId,因为这是一个混合应用程序(Laravel/AngularJS),我无法将其存储在Javascript 查看$localStorage的更改,javascript,angularjs,angularjs-scope,local-storage,ng-storage,Javascript,Angularjs,Angularjs Scope,Local Storage,Ng Storage,我使用的是NGS存储模块,可在此处找到: 我正在localStorage中为实时更新社交提要设置lastSeenId,因为这是一个混合应用程序(Laravel/AngularJS),我无法将其存储在$scope中 MyStatuseController每隔X秒获取一个数组或数据(如果没有可查看的数据,则不获取任何内容),我获取数组的最后一项,并将My$localStorage变量设置如下: $localStorage.lastStatusSeen=lastId 我的导航控制器是我为用户设置一些新
$scope
中
MyStatuseController
每隔X秒获取一个数组或数据(如果没有可查看的数据,则不获取任何内容),我获取数组的最后一项,并将My$localStorage
变量设置如下:
$localStorage.lastStatusSeen=lastId代码>
我的导航控制器是我为用户设置一些新计数器的地方,以查看他们是否遗漏了任何内容,我得到的lastSeenId如下所示:
$scope.$storage.lastStatusSeen
,它被发送到我的API以返回许多未看到的状态帖子
这一切都是可行的,用户处于状态页面,如果有新数据,则将本地存储变量设置为数据数组中的最后一个ID
问题是myNavigationController
没有找到此更改,并且总是传递用户第一次访问页面时传递的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;
});
}];
您可以使用angular
($scope
或$rootScope
)的任何作用域实体的$watch
方法进行观察:
请注意性能,因为它几乎每一个$digest
周期都会运行。另一个注意事项是,您可以将此观察器与变量关联,以便在需要时随时取消它:
var watchIdStored = $scope.$watch(function () {
/* ... */
如果要删除该变量,请将其作为函数调用:
watchIdStored(); // Removes the $watcher
编辑1:
pollingService.startPolling('statusCount','api/social/new count?lastid='+$scope.lastStatusSeen
)上存在问题。它生成了一个不再更改的字符串。您必须在更改发生时通知该更改,并向轮询提供更新
如果您在监视程序中放置一个控制台,在其中更新var,您将看到它正在更新
编辑2:
由于您有一个stopPolling()
方法,您可以在控制器上创建一个build/destroy函数
function _setupPolling (name, url, pollingTime, callback) {
pollingService.stopPolling(name);
pollingService.startPolling.apply(pollingService, arguments);
}
现在,在$localStorage.lastStatusSeen
上发生的每一个更改上都调用它:
$scope.$watch(function () {
return $localStorage.lastStatusSeen;
}, function (newVal, oldVal) {
if (!newVal) {
return;
}
if (newVal !== oldVal) {
console.log(newVal);
// Assign to a local scoped var, or anything, here...
$scope.lastStatusSeen = newVal;
// I suggest call a method to do your logic outside this
_setupPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen, 5000, function(response) {
$scope.socialCount = response.data;
});
}
});
这似乎不起作用,我正在按照上面代码的建议进行操作,我正在将它分配给一个作用域变量,将我的AJAX调用仍显示ID 23,当本地存储显示24?问题更新时,我将它放置在导航控制器中?但如果我将pollingService调用放在我的watcher中,这只会在ID更改后调用它?该函数ion每隔5秒左右轮询一次服务器?好的,我该如何更新我的服务?我需要添加什么方法,这是我的服务代码:对不起,我分心了!的确如此,伙计,非常感谢,省去了我这么多的麻烦!
$scope.$watch(function () {
return $localStorage.lastStatusSeen;
}, function (newVal, oldVal) {
if (!newVal) {
return;
}
if (newVal !== oldVal) {
console.log(newVal);
// Assign to a local scoped var, or anything, here...
$scope.lastStatusSeen = newVal;
// I suggest call a method to do your logic outside this
_setupPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen, 5000, function(response) {
$scope.socialCount = response.data;
});
}
});