I encountered an issue while developing a mobile app with angularjs.
During a request to retrieve a book
object, I noticed a field named book_category
, which holds the category ID for the book
.
To display the name of the category instead of just the ID, I need to make another request to fetch the associated category
. Essentially, I require a SQL JOIN-like operation.
This is how I structured my controller:
booksControllers.controller('BookDetailCtrl', ['$scope', '$routeParams', 'Book', 'Category',
function($scope, $routeParams, Book, Category) {
var book = Book.get({id: $routeParams.bookId});
$scope.book = book;
$scope.category = Category.get({categoryId: book.book_category});
}]);
Here's the relevant service code:
var booksServices = angular.module('booksServices', ['ngResource']);
booksServices.factory('Book', ['$resource',
function($resource){
return $resource('http://****.herokuapp.com/books/:id.json', {}, {});
}]);
booksServices.factory('Category', ['$resource',
function($resource){
return $resource('http://****.herokuapp.com/categories/:categoryId.json', {}, {});
}]);
The issue arises when AngularJS expects an object but receives an array. This happens because AngularJS sends a request to categories.json
instead of something like categories/<id>.json
, despite providing the necessary categoryId
.
Is there any way to work around this problem?
Thank you