Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send object in ejs as function parameter

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>
    <% }) %>  
    <% } %>
like image 659
Ashutosh Jha Avatar asked Jan 24 '26 09:01

Ashutosh Jha


1 Answers

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 %>)"
like image 112
Joost Vunderink Avatar answered Jan 26 '26 01:01

Joost Vunderink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!