How to save ng-show boolean value into the $localStorage or ngStorage?

Hi can you help me, is there any solution how to save ng-show boolean vaue into the $ocalStorage or ngStorage?

I have an example of my code here but it’s not working, Please help me:

example.controller("ExampleController", function($scope,$localStorage) {
 
  $localStorage.showDiv;


  $scope.showPermanently = function(){
    $scope.showMe = true; // set the ng-show value into true
    $localStorage.showDiv = $scope.showMe; / /transfer the showMe value into ngStorage


  }

   $scope.hidePermanently = function(){
    $scope.showMe = false; // set the ng-show value into true
    $localStorage.showDiv = $scope.showMe;/ /transfer the showMe value into ngStorage


  }
   
    
   
 
});



 <ion-content ng-controller="ExampleController">
  
  <br><br><br>

  <button class="button" ng-click="hidePermanently()">Hide Permanently</button>

  <button class="button" ng-click="showPermanently()">Show Permanently</button>

    <div ng-show="showMe">
      Hi I'm Saved Permanently !
    </div>

  </ion-content>

You probably only need to $scope.showMe = $localStorage.showDiv; in the beginning of your controller so it gets the initial value from localStorage.

1 Like

Wow ! Great Thanks You saved my life :smiley:

How about rootScope , are they also the same as scope in saving the boolean value into the $localStorage?

rootScope is the scope above all scopes. You can read more about it here: https://docs.angularjs.org/api/ng/service/$rootScope

But beware, scope is not localStorage. scopes are just the data container for the controllers and templates. Everything localstorage has to happen in a controller, that saves data to localStorage. scope ist then only used to be able to display it in a template.