How do I save tags locally to my device?

I am trying to store tags with local storage. It doesn't work and I don't know what's going on with it.

app.js:

var app = angular.module('plunker', ['ngTagsInput']);

app.controller('MainCtrl', function($scope, $http) {
  $scope.tags = [
    { text: 'Tag1' },
    { text: 'Tag2' },
    { text: 'Tag3' }
  ];
});

      

index.html

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

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="http://mbenford.github.io/ngTagsInput/css/ng-tags-input.min.css" />
    <script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.15"></script>
    <script src="http://mbenford.github.io/ngTagsInput/js/ng-tags-input.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <tags-input ng-model="tags"></tags-input>
    <p>Model: {{tags}}</p>
  </body>

</html>

      

I'm trying to do this using localStorage so that the tags stay there when the user leaves the app, returns to another page, or just refreshes it.

window.localStorage['name'] = {{tags}};

      

Document link: http://learn.ionicframework.com/formulas/localstorage/

+3


source to share


2 answers


You need to store your tag model in localStorage and update it every time the collection changes: remove / add tags. To do this, it is convenient to create an auxiliary service:

var app = angular.module('plunker', ['ngTagsInput']);

app.controller('MainCtrl', function($scope, $http, storage) {
    $scope.tags = storage.get('tags') || [
        { text: 'Tag1' },
        { text: 'Tag2' },
        { text: 'Tag3' }
    ];

    $scope.$watchCollection('tags', function(tags) {
        storage.set('tags', tags);
    });
});

app.factory('storage', function() {
    return {
        get: function(key) {
            return JSON.parse(localStorage[key] || 'null');
        },
        set: function(key, value) {
            window.localStorage[key] = JSON.stringify(value);
        }
    };
});

      

Notice how in your controller code, you check the stored items first and, if not available, drop the tags by default:



$scope.tags = storage.get('tags') || [
    { text: 'Tag1' },
    { text: 'Tag2' },
    { text: 'Tag3' }
];

      

Demo: http://plnkr.co/edit/jgJNADUuTsgbTNvK16Jg?p=preview

+1


source


This code will read tags from localStorage, put it in controller

$scope.tags = JSON.parse(window.localStorage['tags'] || '[]');

      

This code will store the tags in localStorage:



window.localStorage['tags'] = JSON.stringify($scope.tags);

      

For example, a controller would look like this:

app.controller('MainCtrl', function($scope, $http) {
   $scope.tags = JSON.parse(window.localStorage['tags'] || '[]');

  //Another methods
});

      

0


source







All Articles