Alex Man
Alex Man

Reputation: 4886

creating javascript array along with variables in local storage - AngularJS

Can anyone please tell me how to create arrays like myarray = [] along with other variables in localstorage using angularJS, ive created variable which is as shown below.

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="[email protected]" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
    <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>

    <script>
      angular.module('app', [
        'ngStorage'
      ]).

      controller('Ctrl', function(
        $scope,
        $localStorage
      ){
        $scope.$storage = $localStorage.$default({
          x: 42  // variable
        });
      });
    </script>
  </head>

  <body ng-controller="Ctrl">
    <button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button> + <button ng-click="$storage.y = $storage.y + 1">{{$storage.y}}</button> = {{$storage.x + $storage.y}}
  </body>

</html>

Upvotes: 0

Views: 710

Answers (1)

JQuery Guru
JQuery Guru

Reputation: 6963

It support json, you can store anything except those not supported by JSON:

Demo

<script>
      angular.module('app', [
        'ngStorage'
      ]).

      controller('Ctrl', function(
        $scope,
        $localStorage
      ){
        $scope.$storage = $localStorage.$default({
          x: 42,
          array:[]
        });
      });
    </script>

Upvotes: 1

Related Questions