ashish kumar ashish kumar - 1 year ago 177
AngularJS Question

Unexpected token ;

I am new to angularjs, I have an issue that Data is disappear as page got refreshed, i search and find a similar question posted in stackoverflow, that i referred it Data will disappear after page refresh,

my factory service

app.factory("MoreInfoOnChallengeSvc", ["$window", function ($window) {
var data = localStorage.getItem("data")? JSON.parse(localStorage.getItem("data")) || {};
function setChallengeId(id){
data = id;
localStorage.setItem("data", JSON.stringify(data));
function getData(){
return data;
setChallengeId: setChallengeId,
getData: getData


error :

EarnfitApp.js:240 Uncaught SyntaxError: Unexpected token ;
jquery-migrate-1.1.0.min.js:1'//@ sourceURL' and '//@ sourceMappingURL' are deprecated, please use '//# sourceURL=' and '//# sourceMappingURL=' instead.
angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module earnfitApp due to:
Error: [$injector:nomod] Module 'earnfitApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.$injector/nomod?p0=earnfitApp
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:68:12
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2082:17
at ensure (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2006:38)
at module (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2080:14)
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4617:22
at forEach (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:321:20)
at loadModules (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4601:5)
at createInjector (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4523:19)
at doBootstrap (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:1758:20)
at bootstrap (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:1779:12)$injector/modulerr?p0=earnfitApp&p1=Error…t%3A2000%2Fearnfitangular%2Fangular%2Fassets%2Fjs%2Fangular.js%3A1779%3A12)

I don't know what i did wrong

Answer Source

You used the ternary operator the wrong way. You can use it like this:

var data = localStorage.getItem("data") ? JSON.parse(localStorage.getItem("data")) : {};

Or keep the || pattern without ternary operator:

var data = JSON.parse(localStorage.getItem("data")) || {};

This will work because JSON.parse(null) returns null.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download