Reputation: 1173
I have an angular.js app that has a productsCtrl.js that looks like:
app.controller('ProductsCtrl', ['$scope', 'Api', function($scope, Api) {
$scope.products = Api.Product.query();
//Delete Product
$scope.deleteProduct = function(productId, idx) {
Api.Product.remove({productId: productId});
$scope.products.splice(idx, 1);
};
}]);
and the Api factory:
app.factory('Api', ['$resource', function($resource) {
return {
Product: $resource(
'/api/products/:productId',
{productId: '@productId'},
{'query': {method: 'GET', isArray: false }}
),
Item: $resource(
'/api/items/:itemId',
{itemId: '@itemId'}
)
};
}
]);
If I change to $scope.products = Api.Product.get();
, and try the splice method it says TypeError: Object #<Resource> has no method 'splice'
.
If I keep the code the same (just like how the code is displayed above but change the isArray to true, then I get the error: Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
Upvotes: 0
Views: 80
Reputation: 42669
Well one thing is clear, you get method on Product
resource returns single item and not an array. So you should not name your controller property products
. It should be
$scope.product = Api.Product.query();
//Delete Product
$scope.deleteProduct = function(productId, idx) {
Api.Product.remove({productId: productId});
$scope.product=null;
};
Upvotes: 1