I am trying to figure out how to pass an Id into my PlayerDetailController
and then use it in my service.
I have the following route in my main app.js
:
var players = require('./routes/players');
app.use('/players', players);
And I have the following routes inside of the players
route (routes/players.js
):
var express = require('express');
var router = express.Router();
/* GET /players listing. */
router.get('/', function(req, res, next) {
res.render('players', { title: 'Players', action: 'list'});
});
/* GET /player details. */
router.get('/:id', function(req, res, next) {
res.render('players', { title: 'Player Details', action: 'details'});
});
module.exports = router;
And the following in my main players.ejs
template:
<!-- other layout elements -->
<% if (action === 'details') { %>
<% include partials/players/details %>
<% } else if (action === 'update') { %>
<% include partials/players/update %>
<% } else if (action === 'remove') { %>
<% include partials/players/remove %>
<% } else { %>
<% include partials/players/list %>
<% } %>
<!-- other layout elements -->
And my deatils.ejs
partial:
<div ng-controller="PlayerDetailsController">
<div class="playerDetails">
<p>
<strong>{{ player.name}} - {{ player.position }} - {{ player.team }}</strong><br/>
Touchdowns: {{ player.touchdowns }}<br/>
Yards: {{ player.yards }}
</p>
</div>
</div>
And my PlayerDetiails service:
// PlayerDetails Service
app.factory('PlayerDetails', ['$http', function($http){
// below is where I'm trying to inject the Id
return $http.get('/api/player/:id');
}]);
But I have no idea how I can pass the Id that shows up in my route (ex. http://localhost:3000/players/550130d32f3345bc065f2ecf
) into my controller so that my controller calls and receives the correct player data. How would I do this? I thought I could add a id
property to the res.render
call, but I'm not sure what I would do with it in the EJS template. Any help would be greatly appreciated!