Angular Noob:$资源没有返回记录?

时间:2014-07-25 21:06:38

标签: angularjs

我在Rails + Angular上。我在rails中创建了一个路由

返回JSON的

http://localhost:3000/api/products/30

{"id":30,"name":"blue / red / sky blue square eyeglasses","active":true,"page_title":"","meta_description":"","created_at":"2014-05-27T20:26:03.000Z","updated_at":"2014-07-25T14:42:19.000Z","vendor_id":4,"short_description":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum do","long_description":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum do","shape_id":4,"material_id":1,"category_id":3,"taxable":false,"products_colors":[{"id":31,"color_id":4,"product_id":30,"mens":true,"womens":true,"created_at":"2014-05-27T20:26:03.000Z","updated_at":"2014-05-28T19:05:13.000Z","mens_sort_order":1,"womens_sort_order":0,"color":{"id":4,"name":"sky blue","created_at":"2014-05-16T18:11:03.000Z","updated_at":"2014-05-16T18:11:03.000Z","image":{"url":"/images/no-image.png"}}},{"id":36,"color_id":1,"product_id":30,"mens":true,"womens":true,"created_at":"2014-05-28T20:19:40.000Z","updated_at":"2014-05-28T20:19:40.000Z","mens_sort_order":null,"womens_sort_order":null,"color":{"id":1,"name":"blue","created_at":"2014-05-16T17:01:26.000Z","updated_at":"2014-07-25T18:49:54.000Z","image":{"url":"/uploads/color/1/blackt.jpg"}}},{"id":37,"color_id":3,"product_id":30,"mens":false,"womens":true,"created_at":"2014-05-28T20:20:15.000Z","updated_at":"2014-05-28T20:20:15.000Z","mens_sort_order":null,"womens_sort_order":null,"color":{"id":3,"name":"red","created_at":"2014-05-16T18:10:25.000Z","updated_at":"2014-07-25T18:59:17.000Z","image":{"url":"/images/no-image.png"}}},{"id":38,"color_id":5,"product_id":30,"mens":true,"womens":true,"created_at":"2014-05-28T20:21:58.000Z","updated_at":"2014-05-28T20:21:58.000Z","mens_sort_order":null,"womens_sort_order":null,"color":{"id":5,"name":"pitch black","created_at":"2014-05-22T18:04:25.000Z","updated_at":"2014-05-22T18:04:25.000Z","image":{"url":"/images/no-image.png"}}}]}

但是,当我尝试使用$ resource以角度检索此JSON时,它返回Resource

提供者(我应该使用提供者吗?):

app.provider("SingleProduct", function() {
  this.$get = ['$resource', function($resource) {
    return {
      find: function(path) {
        var Product = $resource("/api" + path);
        return Product;
      }
    }
  }];
});

控制器:

app.controller('ProductCtrl', ['$scope', '$location', '$resource', 'SingleProduct', function($scope, $location, $resource, SingleProduct) {

  // $location.path is equal to /products/30
  var product = SingleProduct.find($location.path());
  $scope.title = "fff";
  $scope.product = product;
}]);

视图:

<div ng-controller="ProductCtrl">
  {{ title }} // prints out fff (CORRECT)
  {{ product.name }} // prints out Resource (INCORRECT, should be "blue / red ...")
</div>

发生了什么?当我为/api/products执行此操作时,此技术可用于返回JSON格式的产品数组。

1 个答案:

答案 0 :(得分:2)

您实际上从未在资源上调用任何方法。您的.find返回具有指定路径的资源对象,现在对所述资源执行某些操作:

var product = SingleProduct.find($location.path());
var products = product.get(); //something like this