【发布时间】:2014-05-27 16:12:07
【问题描述】:
我一直在通过以下教程来了解 AngularJS: http://jphoward.wordpress.com/2013/01/05/end-to-end-web-app-in-under-an-hourpart-3/
改编本教程后,我收到以下错误,正在努力寻找解决方案。我的理解是它与工厂有关,最初我认为这可能是一个格式错误的 API 请求,但我似乎在浏览器中得到了正确的结果。
错误:[$resource:badcfg] 资源配置错误。预期响应包含一个对象但得到一个数组
app.js 代码为:
//Main App
var app = angular.module("FixturesApp", ['ngRoute', 'ngResource']).
config(function ($routeProvider) {
//Routing
$routeProvider
.when('/', { templateUrl: 'list.html', controller: ListCtrl })
.otherwise({ redirectTo: '/' });
});
//Factory
app.factory('Fixture', function ($resource) {
return $resource('/api/fixtures/:id', { id: '@id'}, { update: { method: 'PUT' } });
});
//Directive
app.directive('sorted', [
function() {
return {
scope: true,
restrict: 'A',
transclude: true,
template: '<a class="btn btn-link" ng-click="do_sort()" ng-transclude></a>' +
'<span ng-show="do_show(true)">' +
'<i class="glyphicon glyphicon-arrow-down"></i>' +
'</span>' +
'<span ng-show="do_show(false)">' +
'<i class="glyphicon glyphicon-arrow-up"></i>' +
'</span> ',
controller: function($scope, $element, $attrs) {
$scope.sort_by = $attrs.sorted;
$scope.do_sort = function() {
$scope.sort($scope.sort_by);
};
$scope.do_show = function(is_desc) {
return (is_desc != $scope.is_desc && $scope.sort_order == $scope.sort_by)
}
}
};
}
]);
//Controller
var ListCtrl = function ($scope, $location, Fixture) {
$scope.sort_order = "Id";
$scope.is_desc = false;
$scope.sortString = function (is_desc) {
return is_desc ? "-" : "";
}
$scope.sort = function (col) {
if ($scope.sort_order === col) {
$scope.is_desc = !$scope.is_desc;
} else {
$scope.is_desc = false;
$scope.sort_order = col;
}
$scope.search($scope.is_desc);
}
$scope.search = function (is_desc) {
var fixtureList = Fixture.get({ order: $scope.sortString(is_desc) + $scope.sort_order }, function() {
$scope.items = fixtureList.results;
});
}
$scope.search(false);
}
【问题讨论】:
-
您尝试连接的 API 是什么?它返回什么?你能给出 /api/fixtures/:id 的完整路径吗?
-
完整的 URL 是localhost:1062/api/fixtures。我还没有发布它,我正在使用 MVC 的 Web API。
标签: angularjs