【问题标题】:How to send object in ejs as function parameter如何在 ejs 中将对象作为函数参数发送
【发布时间】:2016-03-28 09:02:30
【问题描述】:

如何在 ejs 中将对象作为参数发送。当我尝试在测试函数的参数中发送对象时,它会打印 undefined 。

</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>
    <% }) %>  
    <% } %>

【问题讨论】:

  • 不清楚你在问什么,发给谁和什么对象?
  • 将对象作为参数发送到角度测试函数。这里:ng-click='test()'
  • 看起来你正在做的应该可以正常工作。用户的姓名和电子邮件地址是否显示在表格中?你能展示你的 AngularJS test 函数吗?
  • alert 带有 undefined : app.controller('cntr',function($scope , $http){ $scope.test=function(obj){ alert(obj);} });

标签: angularjs node.js mongodb mean ejs


【解决方案1】:

看起来ejs&lt;%= %&gt; 标记内的任何内容都字符串化了。当我尝试你的代码时,我得到了这个错误:

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 函数实际上在这里接收一个对象,而不是一个字符串。

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这不能回答问题。
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多