I have a list of data and a single page where I want to display a single post.
Service:
app.factory('dealerService', function($resource) {
return $resource('files/js/dealer.json', {}, {
query: {method:'GET', params:{id:'@id'}, isArray:true}
});
});
Controller:
app.controller('DealerDetailsCtrl', function ($scope, $routeParams, dealerService) {
$scope.dealer = dealerService.get({id: $routeParams.id});
});
Route:
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/dealer/:id', {templateUrl: 'files/tpl/dealer-details.html', controller: 'DealerDetailsCtrl'}).
otherwise({redirectTo: '/'});
}]);
Template:
TBD: detail view for {{dealer}}
The {{dealer}}
var gives me all the data available in the json file... not just my specific post.
What am I missing? Thanks for the help!
It looks like you are asking your web server for a file, so that is what you are getting back from the web server. Adding an id
parameter to the URL probably has no affect here.
You'll have to parse the contents of the file in JavaScript (e.g., in an Angular filter) to filter the data and extract only the data with the appropriate ID, or
you'll have to change the server side: have the web server run a script when a certain URL is received to parse the contents of the file and filter out the data with the appropriate ID.