2014-12-04 51 views
1

我注意到,我可以使用$ HTTP提供了这种方式:

Auth.login(userdetails).then(function(response){ 
//... 
} 

app.service('Auth', ['$http', function ($http) { 

    AuthService.login = function (credentials) { 

     return $http. 
      post('getauth.php', credentials). 
      then(function (res) { 

       //create the session, etc. 

      }); 

}]); 

注意在HTTP前面的return语句,然后使用()上http而不是成功()。

为什么这样吗?有什么缺点吗?

+0

可能重复[角HTTPPromise](http://stackoverflow.com/questions/16385278/angular-httppromise-difference -between-success-error-methods-and-thens-a) – Anubhav 2014-12-04 07:59:13

+0

我刚刚回答了这个问题http://stackoverflow.com/questions/27287731/angular-http-service-success-error-then-methods/27288310#27288310 – Chandermani 2014-12-04 08:39:03

回答

0

尝试链承诺那样:

$http.post('URL', DATA) 
    .success(function(data, status, headers, config)) {}) // on success callback 
    .error(function(data, status, headers, config) {}); // on error callback 
+0

也许我不清楚。我的代码没有被破坏,它的工作原理。 – reggie 2014-12-04 08:45:00