olivier olivier - 8 months ago 146
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?


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