如何在基于REST的API使用嵌套的资源呢? [英] How to use nested resources in RESTful API?

查看:296
本文介绍了如何在基于REST的API使用嵌套的资源呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的REST API。请帮我列出所有专辑,按用户名排序,如果某些用户点击,显示此用户的所有专辑。这是code我写到现在为止,但它并没有告诉我正确的事情。

这些资源:结果
  HTTP://jsonplaceholder.typi$c$c.com/users 结果
HTTP://jsonplaceholder.typi$c$c.com/albums

这是code我已经写了:

\r
\r

VAR应用= angular.module('圣诞节',['ngResource']) ;\r
\r
app.controller('SecretOperations',['$范围,$资源',函数($范围,$资源){\r
            $ scope.loadPosts =功能(){\r
                \r
                VAR postingsResource = $资源(的http://jsonplaceholder.typi$c$c.com/albums/',{});\r
                $ scope.postings = postingsResource.query();\r
                postingsResource.query()。$ promise.then(功能(张贴){\r
                    $ scope.postings =帖子;\r
                });\r
                VAR useri = $ scope.postings.userId;\r
                VAR numeUser = $资源(的http://jsonplaceholder.typi$c$c.com/users ID = IDU?{IDU:useri});\r
                $ scope.off = numeUser.query();\r
            };\r
            $ scope.loadPosts();\r
                        \r
        }]);

\r

<!DOCTYPE HTML>\r
< HTML NG-应用=圣诞节>\r
< HEAD>< /头>\r
<身体GT;\r
      < D​​IV CLASS =行NG-控制器=SecretOperations>\r
                < D​​IV CLASS =COL-MD-3文本中心NG重复=在帖子专辑>\r
                    < D​​IV>\r
                        \r
                        < p类=TEXT-静音>专辑名称:{{album.title}}< / P>\r
                        < p NG重复=offof在关闭>作者姓名:{{offof.name}}< / P>\r
                    < / DIV>\r
                < / DIV>\r
        < / DIV>\r
    &所述; SCRIPT SRC =htt​​p://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js\"></script>\r
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-resource.min.js\"></script>\r
    &所述; SCRIPT SRC =HTTP://$c$c.angularjs.org/1.2.13/angular.js&GT;&下; /脚本&GT;\r
    &LT;脚本src=\"//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js\"></script>\r
    &所述; SCRIPT SRC =htt​​ps://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js&GT;&下; /脚本&GT;\r
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js\"></script>\r
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js\"></script>\r
    &LT;脚本的src =// angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js\"></script>\r
    &所述; SCRIPT SRC =app.js&GT;&下; /脚本&GT;\r
&LT; /身体GT;\r
&LT; / HTML&GT;

\r

\r
\r

感谢您


解决方案

您没有使用 $资源服务正常。我建议使用 $承诺属性:

  postingsResource.query()。$ promise.then(功能(张贴){
    $ scope.postings =帖子;
});

  nameUser.get()。$ promise.then(功能(用户){
    $ scope.authorn =用户;
});

在未来,请仔细阅读documentaton,之前提出标准问题在这里:

https://docs.angularjs.org/api/ngResource/service/与美元;资源

P.S:在你的情况,你可能需要链中的两个资源的承诺,因为你需要在第二个第一资源的结果。

I am new to REST API. Please help me to list all Albums, ordered by User name and if some User is clicked, show all albums of this user. This is the code I've written until now but it doesn't show me the right thing.

These are resources:
http://jsonplaceholder.typicode.com/users
http://jsonplaceholder.typicode.com/albums

And this is the code I've written:

var app = angular.module('christmas', ['ngResource']);

app.controller('SecretOperations', ['$scope', '$resource', function($scope, $resource) {
            $scope.loadPosts = function() {
                
                var postingsResource = $resource('http://jsonplaceholder.typicode.com/albums/', {});
                $scope.postings = postingsResource.query();
                postingsResource.query().$promise.then(function(postings){
                    $scope.postings = postings;
                });
                var useri = $scope.postings.userId;
                var numeUser = $resource('http://jsonplaceholder.typicode.com/users?id=idu', { idu : useri });
                $scope.off = numeUser.query();
            };
            $scope.loadPosts();
                        
        }]);

<!DOCTYPE HTML>
<html  ng-app="christmas">
<head></head>
<body >
      <div class="row" ng-controller="SecretOperations">
                <div class="col-md-3 text-center" ng-repeat="album in postings">
                    <div>
                        
                        <p class="text-muted">Album title: {{ album.title }}</p>
                        <p ng-repeat="offof in off"> Author name : {{ offof.name }} </p>
                    </div>
                </div>
        </div>    
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-resource.min.js"></script>
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
    <script src="app.js"></script>
</body>
</html>

Thank you

解决方案

You are not using $resource service properly. I'd suggest to use the $promise property:

postingsResource.query().$promise.then(function (postings) {
    $scope.postings = postings;
});

and

nameUser.get().$promise.then(function (user) {
    $scope.authorn = user;
});

In the future, please carefully read the documentaton, before asking standard questions here:

https://docs.angularjs.org/api/ngResource/service/$resource

P.S.: In your case you might need to chain the two resource promises, because you need the result of the first resource in the second.

这篇关于如何在基于REST的API使用嵌套的资源呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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