how to send object as paremeter in ejs . when i try to send object in parameter for test function it prints 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>
<% }) %>
<% } %>
It looks like ejs stringifies whatever is inside the <%= %> tag. When I tried your code, I got this error:
Syntax Error: Token 'Object' is unexpected, expecting []] at column 16 of the expression [test([object] starting at [{4}].
So I can't reproduce your "undefined" behaviour.
However, it looks like there is a way for it to work:
<button ng-click="test(<%= JSON.stringify(usr) %>)">
The test function actually receives an object here, not a string.
Alternatively, you could only pass the id of the user to the delete function, like this:
ng-click="test(<%= usr.id %>)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With