olivier olivier - 1 year ago 312
AngularJS Question

How to watch for local storage change

Before you mark this question as duplicate, please note that I don't use the

service from angular.

How can I watch for a local storage change?

What i have now is this:

var isUnlocked = window.localStorage.getItem('isUnlocked');
if(isUnlocked === "true") {
$scope.$apply(function () {
$scope.unlocked = true;

The problem right now is that the change is logically first after a refresh visible. How can I change this?

Answer Source

Use $scope.$watch with a function that returns the localStorage value you wish to observe.

function getValue(){
    return window.localStorage.getItem('isUnlocked');

$scope.$watch(getValue, function(newValue){
    if (newValue === "true"){
        $scope.$apply(function(){ $scope.unlocked = true; });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download