I've created a nested resources in my backend. Eg. /resource/:id_resource/subResource/:id_subResource I'm new in angular.js and I'm trying to find the best way to do this. I've used the services and angular-resource components and I need to understand how to pass the first part of the URL with the /resource/:id_resource/ in this correct format (not as normal parameter).
projectsServices.factory('SubResource', ['$resource','$location',
function($resource, $location){
return $resource($location.$$path , {}, {
query: {method:'GET', params:{}, isArray:true}
});
}]);
this is basically tied to the route
.when('/**resource**/:id_resource/**subResource**/:id_subResource', {
templateUrl: '/partials/[...].html',
controller: '[...]DetailsCtrl'
so the URL requested to the API is strictly tied with $location. It works but I think is not the best way to do it. Any thoughts about it?
I think you're looking for is just $routeParams
- something like.
factory('SubResource', ['$resource', function ($resource){
return $resource('/api/:param_one/sub/:param_two',
{param_one:"@_id", param_two: "@id_sub"},
{
update: {
method: 'PUT'
}
});
}]);
Then in your controller
app.controller('Ctrl',function($scope, $routeParams
...
SubResource.get({
param_one: $routeParams.thingId,
param_two: $routeParams.thingId_sub
},
function(data){...});
This pull the params from your route
.when('thing/:thingId/sub/:thingId_sub', {
And after, if your query is your model you can just update if the parameters @ matches.
SubResource.$update(function(res) {
$location.path('SubResource/' + res._id);
});