简体   繁体   English

如何将url参数传递给快速js中的模板

[英]How to pass url parameter to the template in express js

I have the following code in my server.js: 我的server.js中有以下代码:

app.get('/animal/:id', function (request, response)
{
    Animal.find({_id: request.params.id}, function(err, animals)
    {
        response.render('animal', { animals: animals });
    })
})

I am trying to get the animal id to show up in the template url. 我试图让动物ID显示在模板网址中。

           <table border='1'>
                <tr>
                    <th>Name</th>
                    <th>Actions</th>
                </tr>
            <% for (var i in animals){ %>
                    <tr>
                        <td><a href='/animal/?id=**ID RIGHT HERE** %>><%= animals[i].name %></a></td>
                        <td><a href='#'>Edit</a> <a href='#'>Delete</a></td>
                    </tr>

            <% } %>
            </table>  

If I just put in animals[i]._id, it treats as a string, but if I write it as <%= animals[i]._id %>, the whole column is removed. 如果我只是放入animals [i] ._ id,它会将其视为字符串,但如果我将其写为<%= animals [i] ._ id%>,则会删除整个列。

use the expression, you shouldn't send this as query. 使用表达式,您不应该将其作为查询发送。

<%= animals[i]._id %> will be used as params id in your server.js and the <%= animals[i].name %> as anchor tag name <%= animals[i]._id %>将用作server.js中的params id和<%= animals[i].name %>作为锚标记名称

       <table border='1'>
            <tr>
                <th>Name</th>
                <th>Actions</th>
            </tr>
        <% for (var i in animals){ %>
                <tr>
                    <td><a href='/animal/<%= animals[i]._id %>'><%= animals[i].name %></a></td>
                    <td><a href='#'>Edit</a> <a href='#'>Delete</a></td>
                </tr>

        <% } %>
        </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM