如何在ejs中发送对象作为函数参数

如何在ejs中将对象作为paremeter发送。 当我尝试发送testing函数的参数中的对象,它打印未定义。

</tr> <% userlist.forEach(function(usr){ %> <tr> <td><input readonly type="text" value="<%= usr.name %>"></td> <td><input readonly type="text" value="<%= usr.email %>"></td> <td style="cursor:pointer"> <i title="edit data" style="padding-right:18px" class="fa fa-pencil"></i> <i title="delete" ng-click='test(<%= usr %>)' class="fa fa-trash"></i></td> </tr> <% }) %> <% } %> 

看起来像ejs<%= %>标签内的任何内容都ejs 。 当我尝试你的代码,我得到这个错误:

 Syntax Error: Token 'Object' is unexpected, expecting []] at column 16 of the expression [test([object] starting at [{4}]. 

所以我不能重现你的“未定义”的行为。

但是,它看起来有一个方法来工作:

 <button ng-click="test(<%= JSON.stringify(usr) %>)"> 

test函数实际上在这里接收一个对象,而不是一个string。

或者,您只能将用户的ID传递给删除function,如下所示:

 ng-click="test(<%= usr.id %>)" 

您可以通过渲染模板发送任何数据。

  res.render("myPage",{title:"String",age:Number,userlist:[{},{},{}]}})