AngularJS使用$资源服务。承诺不是由GET请求解决 [英] AngularJS using $resource service. Promise is not resolved by GET request

查看:90
本文介绍了AngularJS使用$资源服务。承诺不是由GET请求解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,像这样的服务:

Let's say a service like this:

   services.factory('User', function($resource){
        return $resource('/rest/usersettings/:username', {}, {
            get:    {method: 'GET'},
            update: {method: 'POST'}
        });
    });

所以它应该像这样被使用:

So it is supposed to be used like this:

        scope.user = User.get( {username: 'bob'}  );    // GET

        console.log( JSON.stringify(scope.user) )       // {"$promise":{},"$resolved":false} 

所以,当我发送GET请求,它会确定,建设这个UR +参数:

So, when I send GET request, it goes OK, building this ur + params:

http://localhost:9000/rest/usersettings/bob

问,为什么我有: {$承诺:{},$决心:虚假}

如果我的GET请求导致JSON响应从服务器返回: {用户名:鲍勃,电子邮件:bob@bobs.com} 那么我期待有我的 scope.user 填补数据。

If my GET request leads to json-response back from the server:{"username":"bob","email":"bob@bobs.com"} then I'm expecting to have my scope.user filled by data.

我应该等待某种方式承诺是准备/解决?

Should I wait somehow promise is ready / resolved ?

推荐答案

User.get({用户名:'鲍勃'})不立即返回您的实际数据。它返回的东西将举行你的数据时,AJAX的回报。在那(在 $承诺),你可以注册一个附加的回调记录您的数据。

User.get( {username: 'bob'} ) does not return your actual data immediately. It returns something will hold your data when the ajax returns. On that (the $promise), you can register an additional callback to log your data.

您可以更改code为:

You can change your code to:

   scope.user = User.get( {username: 'bob'}  );    // GET
   scope.user.$promise.then(function(data) {
       console.log(data);
   });

这篇关于AngularJS使用$资源服务。承诺不是由GET请求解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆