观看$ localStorage进行更改

时间:2015-06-26 19:28:29

标签: javascript angularjs angularjs-scope local-storage ng-storage

我正在使用ngStorage模块,可在此处找到:https://github.com/gsklee/ngStorage

我在localStorage中设置了一个lastSeenId用于实时更新社交Feed,因为这是一个混合应用程序(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;
    });

}];

1 个答案:

答案 0 :(得分:1)

您可以使用$watchangular$scope)的任何范围实体的$rootScope方法观看它:

$scope.$watch(function () {
    return $localStorage.lastStatusSeen;
}, function (newVal, oldVal) {
    if (!newVal) {
       return;
    }

    if (newVal !== oldVal) {
        // Assign to a local scoped var, or anything, here...
        // I suggest call a method to do your logic outside this

        $scope.onIdChange(newVal, oldVal); // If you need oldVal
    }
});

小心性能,因为它几乎每$digest个周期运行一次。另一个注意事项是,您可以将此观察者与变量相关联,这样您就可以随时取消它:

var watchIdStored = $scope.$watch(function () {
    /* ... */

如果要删除它,请将var作为函数调用:

watchIdStored(); // Removes the $watcher

编辑1:

pollingService.startPolling('statusCount', 'api/social/new-count?lastid=' + $scope.lastStatusSeen存在问题。它构建了一个不再更改的字符串。您必须在发生更改时通知此更改,为轮询提供更新。

如果你把一个控制台放在观察者里面,你更新了var,你会发现它正在更新。

编辑2:

由于您有stopPolling()方法,您可以在控制器上创建构建/销毁功能。

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;
        });
    }
});