2014-09-23 47 views
0

我想實現的角度與貓鼬,而我一直在使用創建服務期間給錯誤:angularjs功能

test.factory('loginService', function($http){ 
    return 
    { 
     login:function(user) 
     { 
      var $promise = $http.post('data/user.js', user); 
      $promise.then(function(msg){ 
      if(msg.data == 'success') console.log('success login') 
       else 
      console.log('login failed'); 
      }) 
     } 
    } 
}); 

但是,當我嘗試運行的代碼,它提供了錯誤爲:

login:function(user) 
        ^
SyntaxError: Unexpected token (
    at Module._compile (module.js:439:25) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.runMain [as _onTimeout] (module.js:497:10) 
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) 

回答

1

你能嘗試:

test.factory('loginService', function($http){ 

    var loginService = {}; 

    loginService.login = function(user) { 
      var $promise = $http.post('data/user.js', user); 
      $promise.then(function(msg){ 
       if(msg.data == 'success') { 
       console.log('success login') 
       } 
       else { 
       console.log('login failed'); 
       }; 
      }); 
     }; 

    return loginService; 
}); 

我相信{}有一個問題你如果{}其他{};

0

return{必須位於同一行,或者它只是從函數中不返回任何內容。

return {