获取EJS模板中的项目ID并注入服务

时间:2015-03-15 03:02:34

标签: angularjs node.js express ejs

我正在尝试弄清楚如何将ID传递到我的PlayerDetailController,然后在我的服务中使用它。

我的主app.js中有以下路线:

var players = require('./routes/players');
app.use('/players', players);

我在players路线(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;

我的主players.ejs模板中包含以下内容:

<!-- 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 -->

我的deatils.ejs部分:

<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>

我的PlayerDetiails服务:

// PlayerDetails Service
app.factory('PlayerDetails', ['$http', function($http){
    // below is where I'm trying to inject the Id
    return $http.get('/api/player/:id');
}]);

但我不知道如何将我的路线中显示的ID(例如http://localhost:3000/players/550130d32f3345bc065f2ecf)传递到我的控制器中,以便我的控制器调用并接收正确的播放器数据。我该怎么做?我以为我可以在id电话中添加res.render属性,但我不确定在EJS模板中我会用它做什么。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

传递给路线的ID将在req.params.id中提供。您可以使用ID从数据库中检索播放器,并将对象传递给视图。

使用Mongoose ODM这样的东西。如果您使用的是其他数据库/ ODM / ORM,则代码会有所不同,但想法是一样的:

/* GET /player details. */
router.get('/:id', function(req, res, next) {
    Player.find({ _id: req.params.id }, function(err, player) {
        if(err) return next(err);
        res.render('players', { title: 'Player Details', action: 'details', player: player });
    });
});

然后您可以通过player属性访问播放器文档。

如果你想创建一个能以JSON格式返回所有玩家数据的API,你可以这样做:

/* GET /api/player/:id */
router.get('/:id', function(req, res, next) {
    Player.find({ _id: req.params.id }, function(err, player) {
        if(err) return next(err);
        res.json(player);
    });
});

答案 1 :(得分:1)

将ID传递给您的模板:

router.get('/:id', function(req, res, next) {
    res.render('players', { title: 'Player Details', action: 'details', id: req.params.id});
});

ng-init收到id:

<div ng-controller="PlayerDetailsController" ng-init="id = <%= id %>">

PlayerDetails服务:

app.service('PlayerDetails', ['$http', function($http){
    return {
         get: function(id) {
           return $http({
            url: '/api/player/:id',
            params: {id: id},
            method: 'GET'
           });
         }
    };
}]);

PlayerDetailsController中的某个地方:

// id is from ng-init
PlayerDetails.get($scope.id).success(function(player) {
   $scope.player = player;
}).error(function(error) {
   console.error(error);
});