2016-02-01 27 views
0

我想實現的角度資源用於登錄這樣我如何通過回撥到角資源保存方法

var data = new Login({    

      username: user.userName, 
      password: user.password 
     }) 
data.$save() 

這是假設一些數據返回給我,如果登錄成功,或者如果返回錯誤不是這樣。

我想要的是像這樣的角度http post方法的回調。

data = JSON.stringify({ 
      username: user.userName, 
      password: user.password 
         }) 
    $http.post('API/sigin',data, 

      { 
       headers: { 
        'Content-Type': 'application/json' 
       } 
      } 

) 
    .success(
     function(response){ 

    // success callback 

     console.log("doing sign in"); 

    }, 
     function(error){ 


     // failure callback 

     return error 


     } 
     ) 

我切換到資源時,http post失敗了我。它只會永久掛起,並在稍後返回錯誤。

我正在使用角1.4.3。

任何幫助,信息可以理解

回答

0

..factory.js

//Create a factory for the Login API 
angular.module(...) 
.factory('LoginEntity', ['$resource', function ($resource) { 
    return $resource(
     'API/sigin', 
     { 
      save: {method: 'POST'}, 
      update: {method: 'PUT'} 
     } 
    ); 
}]) 

... controller.js

angular.module(...) 
     .controller('xxxController', ['LoginEntity', function(LoginEntity){ 
    //in Controller add LoginEntity dependency 

    LoginEntity.save({ 
          username: user.userName, 
          password: user.password 
         }, 
         function (response) { 
          //success callback 
         }, 
         function() { 
          //error callback 
          //usually do some logging stuff here 
         }); 

}]); 
+0

謝謝,這似乎是在什麼時候被我記錄將狀態傳遞給成功回調,但是當我要求它返回永久掛起的響應 – kplus

+0

@ huan feng再次LoggerService的含義是什麼?你的意思是說LoginEntity.error? – kplus

+0

您可以忽略LoggerService行,我在那裏寫它,因爲通常我們做一些日誌記錄時,API調用有一些錯誤。 –