Angularjs - multiple tabs in the same browser

Asked

Viewed 661 times

2

I am using Angularjs in my Node application. When I try to open another Tab also with my application in the same browser ( or open another instance of the same browser ) I get this error:

s:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0(anonymous function) @ utils.js:14
angular.js:13236 SyntaxError: Unexpected token u in JSON at position 0
at Object.parse (native)
at new MyController (http://meuProjeto/my.controller.js:8:28)
at Object.invoke (http://meuProjeto/js/libs/angular-1.5.0/angular.js:4604:19)
at extend.instance (http://meuProjeto/js/libs/angular-1.5.0/angular.js:9855:34)
at nodeLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8927:34)
at compositeLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8226:13)
at publicLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8106:30)
at http://meuProjeto/js/libs/angular-1.5.0/angular.js:1696:27
at Scope.$eval (http://meuProjeto/js/libs/angular-1.5.0/angular.js:16820:28)
at Scope.$apply (http://meuProjeto/js/libs/angular-1.5.0/angular.js:16920:25)(anonymous function) @ angular.js:13236

in my script utils.js in line 14 has the following command:

var param = JSON.parse(window.sessionStorage.param);

I know it’s very generic what I posted, but I’m new to WEB development and I’m posting this hoping that someone has been through the same problem (or something like that) and can guide me! I have no idea where to start looking for a solution, posted so too.

If necessary I can add some information that is relevant and I have not put here.

From now on, thank you!

1 answer

2


sessionStorage belongs exclusively to the session browser, which is linked to the page.

If you want to share content between two or more tab/windows, use localStorage.

Example of implementation:

app.factory('userService', ['$rootScope', function ($rootScope) {
    var service = {
        model: {
            name: '',
            email: ''
        },

        SaveState: function () {
            sessionStorage.userService = angular.toJson(service.model);
        },

        RestoreState: function () {
            service.model = angular.fromJson(sessionStorage.userService);
        }
    }

    $rootScope.$on("savestate", service.SaveState);
    $rootScope.$on("restorestate", service.RestoreState);

    return service;
}]);

(source: https://stackoverflow.com/questions/18247130/how-to-store-the-data-to-local-storage)

Below is a list of services that can help you:

  • Thank you for the reply and the explanation Onosendai . I will test your answer and as soon as I finish I will mark it as correct!

  • In the links you provided, I found one of them explaining the use of sessionStorage and localStorage. I changed how you said sessionStorage by localStorage and apparently everything is ok! Thanks for the reply!

  • 1

    I’m glad it worked. Feel free to ask if you have any other questions!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.