简体   繁体   English

如何使用angular-js $ resource?

[英]How do I use an angular-js $resource?

So I'm trying to use the angular-js $resource function while building an app with ionic framework. 所以我试图在使用离子框架构建应用程序时使用angular-js $ resource函数。 But I can't seem to make it work. 但是我似乎无法使其正常工作。 Relevant code: 相关代码:

Service.js Service.js

.factory('Pages', function($resource) {
    var source = $resource("http://localhost:5432/api/pages",{},
    {query: { method: "GET", isArray: false }});
    return source;
})

controllers.js controllers.js

.controller('PagesCtrl', function($scope, Pages) {
$scope.pages =  Pages.query();
$scope.results = $scope.pages.results;
console.log($scope.results);
})

browse.html browse.html

<ion-view view-title="Browse">
  <ion-content>
    <h1>Browser</h1>
    <ion-list>
      <ion-item ng-repeat="page in results">
        <h2>{{page.title}}</h2>
      </ion-item>

    </ion-list>
  </ion-content>
 </ion-view>

and the data 和数据

{
"count": 2,
"next": null,
"previous": null,
"results": [
    {
        "id": 1,
        "parent": null,
        "title": "Blog",
        "content": "<p>Blog</p>",
        "content_model": "richtextpage",
        "slug": "blog",
        "publish_date": null,
        "login_required": false,
        "meta_description": "Blog",
        "tags": ""
    },
    {
        "id": 2,
        "parent": null,
        "title": "Lorem Ipsum",
        "content": "Lorem ipsum ....",
        "tags": ""
    }
]
}

But the console log (in controllers.js) will always print 'undefined'. 但是控制台日志(在controllers.js中)将始终显示“ undefined”。 I've been at it for hours and I can't seem to find what's wrong. 我已经呆了好几个小时了,但似乎找不到什么地方。

$scope.pages is a promise. $ scope.pages是一个承诺。 To write to console you'll need to use a callback function like this: 要写入控制台,您需要使用如下回调函数:

$scope.pages = Pages.query();
$scope.pages.$promise.then(function (result) {
  console.log(results);
});

The actual result object of .query() is a promise that will hold the result at some point. .query()的实际结果对象是一个承诺,它将在某个时候保存结果。

So at the moment you try to access the result property of your payload, it is likely that the request is not yet finish. 因此,当您尝试访问有效负载的result属性时,请求可能尚未完成。

However, Angular ng-repeat statement has a built in support for promises so in your view, browse.html, if you do: 但是,Angular ng-repeat语句具有对promise的内置支持,因此,如果您这样做,则在您的视图中为browse.html。

  <ion-item ng-repeat="page in pages.results">

Instead of dealing with a $scope.results in your controller, everything should looks better. 而不是在您的控制器中处理$scope.results ,一切应该看起来更好。

Here is my suggestion for the service: 这是我对服务的建议:

.factory('Pages', function($resource) {
    return {
       source: $resource("http://localhost:5432/api/pages",{},
                   { 
                      query: { 
                          method: "GET", 
                          isArray: false 
                      }
                    })
    }
})

and then in the ctrl: 然后在ctrl中:

.controller('PagesCtrl', function($scope, Pages) {
    $scope.pages =  Pages.source.query();
    $scope.results = $scope.pages;
    console.log($scope.results);
})

But you know that the query is returning only an object because you tell the $resource that your query doesn't return an Array (it is false). 但是您知道查询仅返回一个对象,因为您告诉$resource您的查询没有返回数组(它为false)。

This only have to work if your Backend is correct defined. 只有在正确定义了后端的情况下,这才起作用。

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

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