什么';我的angularjs工厂出了什么问题?

什么';我的angularjs工厂出了什么问题?,angularjs,Angularjs,我有一个构建应用程序的程序 var HappySundayonEtsyApp = angular.module('HappySundayonEtsyApp', ['ngResource']); 像这样的控制器 HappySundayonEtsyApp.controller('ListingsController', function ListingsController ($scope, $filter, Listings, $resource) { $scope.sortingOrder =

我有一个构建应用程序的程序

var HappySundayonEtsyApp = angular.module('HappySundayonEtsyApp', ['ngResource']);
像这样的控制器

HappySundayonEtsyApp.controller('ListingsController',
function ListingsController ($scope, $filter, Listings, $resource) {
$scope.sortingOrder = sortingOrder;
...
控制器工作正常,在控制器中添加了一些静态JSON进行测试。当我尝试使用定义为的服务获取数据时

HappySundayonEtsyApp.factory('Listings', ['$resource', function ($resource) {
var resource = $resource('/listings/:id', { 
  id : '@id' 
}, {
  index : { method: 'GET', isArray : true },
  save : { method : 'PUT' }
});
return resource;
}]);
在控制器中

$scope.items = Listings.get()
什么都不管用。正如我用一个简单的

$http.get('/listings/').success(function(data){
   $scope.items = data;
});
在调试器(Chrome)中,我看到

我使用的是1.0.3版。如果有人能帮助我,我将不胜感激。经过两天的阅读、尝试和调试,我深感沮丧。可能是一些简单的事情


谢谢。

实际上,您的JSON看起来像是一个数组,您专门定义了
索引
操作 您在其中设置了isArray:true,因此您可能打算:

$scope.items = Listings.index();

当使用
$resource
时,调用
.get
方法不会返回HTTP调用的响应,它只返回一个承诺。这意味着$scope.items将来将包含结果

要确保
$scope.items
包含HTTP/$resource调用的响应,请改为在回调中分配$scope.items:

Listings.get(function (response) {
    $scope.items = response;
});
但是,假设您有:

<ul>
    <li data-ng-repeat="item in items"></li>
</ul>

然后您可以使用此选项:
$scope.items=Listings.get()
,因为当承诺兑现时,重复将被更新并迭代所有项目。

请求转到哪个URL?Try:
Listings.get({id:'11'})
应该生成一个对
/Listings/11
的请求我查看了一下,是的,它生成了一个请求
/Listings/11
,可以在为REST提供服务的Rails应用程序的日志中看到。在浏览器中,它返回正确的JSON,$scope.items再次是
$scope.items:h_uu proto_uuh:h$delete:function(a,b,f){var g=d(this),e=p,j;switch(arguments.length){case 3:g=a;e=b;j=f;break;case 2:case 1:r(a)-(e=a,j=b):..
另外,
索引可能不需要,因为ngResource提供了一种类似的
查询
方法。谢谢,我阅读了promise的概念,并尝试了它,但没有效果。可能是因为我使用了
ng repeat
-与
数据ng repeat
有什么区别吗?
ng repeat
数据ng-repeat
是相同的,
数据ng repeat
是有效的HTML,
ng repeat
不是。但两者的工作原理相同。
<ul>
    <li data-ng-repeat="item in items"></li>
</ul>