简体   繁体   English

AngularJS 使用 $resource 服务。 GET 请求未解决承诺

[英]AngularJS using $resource service. Promise is not resolved by GET request

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} 

So, when I send GET request, it goes OK, building this ur + params:所以,当我发送 GET 请求时,一切正常,构建这个 ur + params:

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

Question, why I have: {"$promise":{},"$resolved":false}问题,为什么我有: {"$promise":{},"$resolved":false}

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.如果我的 GET 请求导致从服务器返回 json 响应: {"username":"bob","email":"bob@bobs.com"}那么我希望我的scope.user被数据填充。

Should I wait somehow promise is ready / resolved ?我应该以某种方式等待承诺准备好/解决了吗?

User.get( {username: 'bob'} ) does not return your actual data immediately. User.get( {username: 'bob'} )不会立即返回您的实际数据。 It returns something will hold your data when the ajax returns.当ajax返回时,它会返回一些东西来保存你的数据。 On that (the $promise ), you can register an additional callback to log your data.在那( $promise )上,您可以注册一个额外的回调来记录您的数据。

You can change your code to:您可以将代码更改为:

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

You will get your data in there, but not immediately.您将在那里获取数据,但不会立即获取。 Read the docs on ngResource :阅读ngResource 上文档

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray).重要的是要意识到调用 $resource 对象方法会立即返回一个空引用(对象或数组取决于 isArray)。 Once the data is returned from the server the existing reference is populated with the actual data.从服务器返回数据后,现有引用将填充实际数据。 This is a useful trick since usually the resource is assigned to a model which is then rendered by the view.这是一个有用的技巧,因为通常资源被分配给一个模型,然后由视图呈现。 Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data.拥有一个空对象会导致没有渲染,一旦数据从服务器到达,对象就会填充数据,并且视图会自动重新渲染自身以显示新数据。 This means that in most cases one never has to write a callback function for the action methods.这意味着在大多数情况下,您永远不必为操作方法编写回调函数。

For now I use this (it seems I duplicate this question )现在我使用这个(似乎我重复了这个问题)

User.get({
    username: 'bob'
}, function(user) {

    user.$update(
        function(data, headers) {
            console.log("GOOD");
        },
        function(err, headers) {
            console.log("BAD");
        }
    );
});

This should work :这应该工作:

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

toJSON() cleans up Angular's internal properties ($$). toJSON() 清理 Angular 的内部属性 ($$)。

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

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