意外的标记 ;

时间:2016-08-03 11:28:27

标签: javascript angularjs

我是angularjs的新手,我有一个问题,当页面被刷新时数据会消失,我搜索并找到在stackoverflow中发布的类似问题,我提到它Data will disappear after page refresh,

我的工厂服务

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;
    }
    return{
     setChallengeId:   setChallengeId,
       getData: getData 
    };

}]);

错误:

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.
http://errors.angularjs.org/1.5.8/$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)
http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=earnfitApp&p1=Error…t%3A2000%2Fearnfitangular%2Fangular%2Fassets%2Fjs%2Fangular.js%3A1779%3A12)

我不知道自己做错了什么

1 个答案:

答案 0 :(得分:1)

你以错误的方式使用了三元运算符。您可以像这样使用它:

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

或者保留||模式而不使用三元运算符:

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

这将有效,因为JSON.parse(null)会返回null