【发布时间】:2016-07-26 07:45:23
【问题描述】:
我是新人,在调用服务后使用资源模块很难得到这个错误。 任何人都可以在我出错的地方修改我的代码错误,或者只是修改了需要纠正的部分,谢谢。
数据格式来了:-
[
brands: Array[1]
0: Object
__v: 0
_id: "5251a4a34f232fc3902"
account_id: "525072320e32971b"
name: "Fruits"
__proto__: Object
1: Object
length: 2
categories: Array[1]
0: Object
__v: 0
_id: "5251a4a34f2323fc3902"
account_id: "5250723230e32971b"
name: "Fruits"
__proto__: Object
1: Object
length: 2
]
错误:-
[$resource:badcfg] 资源配置错误。预期的响应包含一个数组但得到一个对象
itmsFormView.html
<select class="form-control" ng-model="item.brand_id" id="itemBrand" >
<option value="">Select Brand</option>
<option ng-repeat="brand in brands" value="{{brand.brand_id}}">{{brand.name}} </option>
</select>
<select class="form-control" ng-model="item.category_id" id="itemCategory">
<option value="">Select Category</option>
<option ng-repeat="category in categories" value="{{category.brand_id}}">{{category.name}}</option>
</select>
ItemsService.js
app.factory('itemService', function ($resource) {
return {
getCategoryAndBrand : $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
};
});
ItemsController.js
app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){
console.log(response);
},function(errorResponse){
console.log(errorResponse);
}
);
});
【问题讨论】:
-
我可能错了,我必须仔细检查文档,但是根据您的错误,资源函数是否返回一个对象并且需要一个数组?与将依赖项作为数组而不是对象传递给模块的方式相同?
标签: javascript angularjs