So I am trying to get a single "post" object from a list of posts but i don't know the name of passed params object name. Here's post-details component 'use strict';
angular.module('postDetail', ['ngRoute', 'postsService'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/posts/:postId', {
templateUrl: 'post-detail/post-detail.template.html',
controller: 'PostDetailController'
});
}])
.controller('PostDetailController', ['$scope', '$http', 'postsService', '$routeParams', function($scope, $http, postsService, $routeParams) {
$scope.post = postsService.getPost($routeParams.postId);
}]);
Here I am passing postId to the getPost method. BTW, here the value of postId is valid.
This is my Posts Service.
angular.
module('postsService', ['ngResource']).
factory('postsService', ['$resource',
function($resource) {
return $resource('https://jsonplaceholder.typicode.com/posts/:postId', {}, {
getPost: {
method: 'GET',
params: {postId: '@postId'}
}
});
}
]);
But here the "@postId" expression is empty and it tries to get the list of posts. How can i reference the passed parameter?
My post details template
<h3>{{post.title}}</h3>
<p>{{post.body}}</p>
Instead of:
$scope.post = postsService.getPost($routeParams.postId);
do:
$scope.post = postsService.getPost({
postId: $routeParams.postId
});
Check out the example of how to use $resource
class methods in the documentation.