Yash Majithiya
Yash Majithiya

Reputation: 861

How to use Angular Local storage

i'm very much new to angular and trying to access localStorage. Here is the code. Please help me figure that out if it is not the right way to define and use. Thank you.

var app = angular.module("toggleApp", ['LocalStorageModule']).config(function (localStorageServiceProvider) {
        localStorageServiceProvider
            .setPrefix('test')
            .setStorageType('localStorage');
    });
    app.controller("toggleController", function ($scope, $timeout,
                                                 timerParsingService, localStorageService) {
        localStorageService.set('user' ,'yash');

Upvotes: 2

Views: 4353

Answers (2)

Lalit Sachdeva
Lalit Sachdeva

Reputation: 6639

you dont require to define any module or inject any other module in the application.

inject $window in your application and then you can access the localstorage module like this

angular.module('toggleApp', [])
     .controller('toggleController', ['$scope', '$window', function ($scope,  $window) {
    $window.localStorage.setItem('ls', 'test');
}]);

and save the value like this

$window.localStorage.setItem('ls', 'test');

and you can retrieve the localStorage value like this

$window.localStorage.getItem('ls');

Upvotes: 2

user93
user93

Reputation: 1866

you could use ng-storge module and use

$localStorage.key = value or you could also use the getter and setter

$localStorageProvider.set('key', { k: 'value' });

Upvotes: 0

Related Questions