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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20008244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:03:50  来源:igfitidea点击:

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

jsonangularjs

提问by ses

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":"[email protected]"}then I'm expecting to have my scope.userfilled by data.

如果我的 GET 请求导致从服务器返回 json-response:{"username":"bob","email":"[email protected]"}那么我希望我scope.user的数据填充。

Should I wait somehow promise is ready / resolved ?

我应该以某种方式等待承诺准备好/解决了吗?

回答by Kos Prov

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.

User.get( {username: 'bob'} )不会立即返回您的实际数据。当ajax返回时,它会返回一些东西来保存你的数据。在那个 (the $promise) 上,您可以注册一个额外的回调来记录您的数据。

You can change your code to:

您可以将代码更改为:

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

回答by Stewie

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). 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.

重要的是要意识到调用 $resource 对象方法会立即返回一个空引用(对象或数组取决于 isArray)。从服务器返回数据后,现有引用将填充实际数据。这是一个有用的技巧,因为通常资源被分配给一个模型,然后由视图呈现。拥有一个空对象会导致没有渲染,一旦数据从服务器到达,对象就会填充数据,并且视图会自动重新渲染以显示新数据。这意味着在大多数情况下,您永远不必为操作方法编写回调函数。

回答by ses

For now I use this (it seems I duplicate thisquestion )

现在我使用这个(似乎我重复了 这个问题)

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

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

回答by devside

This should work :

这应该工作:

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

toJSON() cleans up Angular's internal properties ($$).

toJSON() 清理 Angular 的内部属性 ($$)。