2014-09-04 69 views
0

,我有以下工廠服務angularjs:語法錯誤的AngularJS工廠服務

'use strict'; 
angular.module('gameApp_services').factory('sessionService', ['$http', function($http) { 
    return { 
     set:function(key, value) { 
      return sessionStorage.setItem(key,value); 
     }, 
     get:function() { 
      return sessionStorage.getItem(key); 
     }, 
     destroy:function() 
      return sessionStorage.removeItem(key); 
     } 
    }; 
}]).factory('loginService', function($http,$location,sessionService) { 
    return { 
     login: function(data, scope) { 
      var $promise = $http.post("lib/action.php", data); //send data to action.php 
      $promise.then(function(msg) { 
       var uid = msg.data; 
       if(uid) { 
        //scope.msgtxt='Correct information'; 
        sessionService.set('user', uid); 
        $location.path('/game'); 
       } else { 
        scope.msgtxt='Incorrect information'; 
        $location.path('/firstpage'); 
       } 
      }); 
     }, 
     logout:function() { 
      sessionService.destroy('user'); 
      $location.path('/firstpage'); 
     } 
    } 
}); 

當我運行它,我得到這個錯誤信息:

SyntaxError: syntax error 


return sessionStorage.removeItem(key); 

應該如何語法是什麼樣子?任何人都可以幫助我?我沒有我的想法。

+1

你錯過了函數的開頭'{'。 – 2014-09-04 11:51:31

回答

0

我發現了這個問題。我在sessionStorage.removeItem中忘了{。

2

您在銷燬後錯過了右括號。

這應該工作。 '

angular.module('gameApp_services').factory('sessionService', ['$http', function($http) { 
    return { 
     set:function(key, value) { 
      return sessionStorage.setItem(key,value); 
     }, 
     get:function() { 
      return sessionStorage.getItem(key); 
     }, 
     destroy:function() { 
      return sessionStorage.removeItem(key); 
     } 
    }; 
}]).factory('loginService', function($http,$location,sessionService) { 
    return { 
     login: function(data, scope) { 
      var $promise = $http.post("lib/action.php", data); //send data to action.php 
      $promise.then(function(msg) { 
       var uid = msg.data; 
       if(uid) { 
        //scope.msgtxt='Correct information'; 
        sessionService.set('user', uid); 
        $location.path('/game'); 
       } else { 
        scope.msgtxt='Incorrect information'; 
        $location.path('/firstpage'); 
       } 
      }); 
     }, 
     logout:function() { 
      sessionService.destroy('user'); 
      $location.path('/firstpage'); 
     } 
    } 
});