对于角度而言,我是一个十足的菜鸟,刚刚完成了代码学校教程,我就遇到了第一个障碍。
我正进入(状态Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
这是我的代码:
var bulletinApp = angular.module('bulletinApp', ['ngResource']);
bulletinApp.controller('PostsController', ['$scope', 'Post', function($scope, Post) {
$scope.heading = 'Welcome';
$scope.posts = Post.query();
$scope.newPost = {
title: 'Test Post'
}
}]);
bulletinApp.factory('Post', ['$resource', function($resource) {
return $resource('/posts');
}]);
我在这里找到了答案:在 AngularJS/Rails 中使用 $resource.query() 函数时资源配置错误 https://stackoverflow.com/questions/20037852/error-in-resource-configuration-while-using-resource-query-function-with-angu
这就是说我应该将其添加到我的资源声明中:
'query': {method: 'GET', isArray: false }
问题是我不知道应该在哪里添加它,或者即使这是我遇到的问题?
EDIT:
bulletinApp.factory('Post', ['$resource', function($resource) {
return $resource('/posts', {'query': {method: 'GET', isArray: false}});
}]);
此后我仍然收到错误。同样在教程中,没有必要这样做,而且我已经完全遵循了它,为什么 get 方法会获取对象而不是数组?