简体   繁体   English

AngularJS-将then()与$ http服务一起使用

[英]AngularJS - Using then() with $http service

I noticed that I can use the $http provider in this way: 我注意到可以以这种方式使用$ 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.

            });

}]);

Notice the return statement in front of http and the use of then() on http instead of success(). 请注意,http前面的return语句以及在http上使用then()而不是success()。

Why does this work? 为什么这样做? Are there any downsides? 有没有缺点?

Try to chain the promise like that: 尝试像这样链接诺言:

$http.post('URL', DATA)
    .success(function(data, status, headers, config)) {}) // on success callback
    .error(function(data, status, headers, config) {}); // on error callback
 $http({
        url: 'put url here',
        method: "put action here just like GET/POST",
        data: { 'name': 'Anil Singh' }
    })
    .then(function (resp) {
        //TODO: put success logic here
    },
    function (resp) {
        //TODO:  put failed logic here
    }
 );

Angular $http service- success(), error(), then() methods 角$ http服务-success(),error(),then()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM