0

I have added some data in local storage in index.js file in javasrcipt using

var account = {
            name: name,
            email: email
            };
            //converts to JSON string the Object Literal
account = JSON.stringify(account);
localStorage.setItem('Account', account);
console.log("cookies"+localStorage.getItem('Account'));

my log returns me data saying cookies{"name":"Chetan shah" , "email":"some email id"}

Now I want to get this item to my controller.js in angularjs.I have used https://github.com/grevory/angular-local-storage this link

inside controller i wrote the below code

var classFinder= angular.module('classFinder',['onsen.directives', 'ngTouch','ngCookies','LocalStorageModule']);
classFinder.controller('courseController',['$scope', '$window', '$http', '$cookieStore','localStorageService',function($scope,$window,$http,$cookieStore,localStorageService){
var myaccount = localStorageService.get('Account');
console.log("cookiestore"+myaccount);

But my console return cookiestore null

How can i achieve the data from index.js to controller.js !!

10
  • have you considered using a service and the $rootscope.$broadcast function ? Commented Mar 1, 2015 at 12:52
  • nope i havent !! How do i do that !! Commented Mar 1, 2015 at 12:54
  • What's the context of where you're storing to localStorage, and why not use localStorageServer instead of accessing localStorage directly? Commented Mar 1, 2015 at 12:54
  • the index.js file is coded in normal javascript and not angularjs Commented Mar 1, 2015 at 12:55
  • @MichalCharemza I dont know how to do that !! :( any example Commented Mar 1, 2015 at 12:56

1 Answer 1

3

I suspect what's happening is that the localStorageService is doing something in addition to just JSON encoding the value. Looking at the source https://github.com/grevory/angular-local-storage/blob/master/src/angular-local-storage.js , it seems to be at least adding a prefix of ls to the keys.

I would say you should use the same interface for setting the value as getting it so, either:

  • Use window.localStorage directly in the controller (and I suspect you might need to decode the JSON as well):

    var classFinder= angular.module('classFinder',['onsen.directives', 'ngTouch','ngCookies','LocalStorageModule']);
    classFinder.controller('courseController',['$scope', '$window', '$http', '$cookieStore','localStorageService',function($scope,$window,$http,$cookieStore,localStorageService){
      var myaccount = $window.JSON.parse($window.localStorage.getItem('Account'));
      console.log("cookiestore" + myaccount);
    
  • or use the localStorageService to set the value from your index.js. If you need access to the context of the app, you can do this from run method:

    angular.module('classFinder').run(['localStorageService', function(localStorageService) {
      var account = {
        name: name,
        email: email
      };
      localStorageService.set('Account', account);
    });
    

    Note you will have to run the above after angular.module('classFinder', [....]) so Angular knows its accessing a module that has already been created, rather than creating a new one. (Having the brackets / not having the brackets [ ] as the second argument to module makes all the difference)

Sign up to request clarification or add additional context in comments.

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.