2

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

2 Answers 2

2

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.