Angular-UI模态错误:期望的响应包含一个对象,但得到一个数组
Angular-UI Modal Error: Expected response to contain an object but got an array
首先,我知道这个错误在这里已经被讨论过很多次了,我仔细阅读了很多关于这个问题的问题,但似乎没有一个完全适合我的问题。为什么?它们中的大多数都与Angular UI模态以及它请求显示资源的方式无关。
我将开始展示我的控制器:
'use strict';
// Customers controller
angular.module('customers').controller('CustomersController',
['$scope', '$stateParams', '$location', '$filter', 'Authentication', 'Customers', '$modal', '$log',
function($scope, $stateParams, $location, $filter, Authentication, Customers, $modal, $log ) {
// Find a list of Customers
$scope.find = function() {
Customers.query(function (data) {
$scope.customers = data;
$scope.buildPager();
});
};
// Find existing Customer
$scope.findOne = function() {
$scope.customer = Customers.get({
customerId: $stateParams.customerId
});
};
//List Pager
$scope.buildPager = function () {
$scope.pagedItems = [];
$scope.itemsPerPage = 15;
$scope.currentPage = 1;
$scope.figureOutItemsToDisplay();
};
/* Modal stuff */
$scope.animationsEnabled = true;
$scope.openUpdateModal = function (size, selectedCustomer) {
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'modules/customers/views/edit-customer.client.view.html',
controller: function ($scope, $modalInstance, upcustomer) {
$scope.upcustomer = upcustomer;
},
size: size,
resolve: {
upcustomer: function () {
return selectedCustomer;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.toggleAnimation = function () {
$scope.animationsEnabled = !$scope.animationsEnabled;
};
//Controller Closing brackets
}
]);
在视图中,我有一个客户列表,所以如果我单击其中一个,我希望模式打开:
<a data-ng-repeat="customer in pagedItems" ng-click="openUpdateModal('lg', customer)" class="list-group-item"></a>
我是根据Bossable.com的一个视频教程做的这件事,叫做:MEAN Stack: 20 - Pass Customer Details to an Angular UI Modal
唯一的区别是,当我点击客户时,我得到了这个错误:
[$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array
感谢您的宝贵时间。
删除$scope.findOne
函数,因为它使用get方法并返回一个数组。如果你回头看看她的教程,你会发现她一直在评论。
您的问题与您的Customer.query()或Customer.get()调用有关。这些是$resource调用,并期望被告知响应是什么。
通常get方法需要一个对象,而query方法需要一个数组。如果你没有为这些方法返回其中一个,你需要根据你的需要使用isArray: true/false选项来配置它。
相关文档:
https://docs.angularjs.org/api/ngResource/service/美元资源
$resource( 'apiendpoint', {}, {
query: {
isArray: false
}
});
}
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- JavaScript数组包含一个值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- jQuery$.inArray()总是返回-1和一个对象数组
- 在数组中的一个元素上设置多个值
- javascript处理一个对象数组以获得一个新的对象数组
- 作为一个二维数组,从ajax接收
- 你能用来自数组的属性名称生成一个对象吗
- 多维关联数组的最后一个索引
- 如何创建一个方法来验证数组的范围
- 循环以检查数组中的最后一个图像
- 在Javascript中将一个值和字符串数组转换为if语句
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 如何将一个对象添加到每个对象数组中
- 如何创建一个谷歌地图地理坐标数组
- 如何从另一个带下划线的数组中筛选带元素的数组
- 使用window.location.htm和匹配的URL数组(一个用于桌面,一个用于移动)将桌面网站重定向到移动
- Javascript排序多维数组-一个完整的例子
- 刽子手的游戏.2数组.一个需要相应地更新另一个