3

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>
    <% }) %>  
    <% } %>
4
  • It's unclear what you are asking, send to who and what object? Commented Dec 22, 2015 at 6:59
  • send a object to angular test function as parameter . here : ng-click='test(<%= usr %>)' Commented Dec 22, 2015 at 7:05
  • It looks like what you are doing should work fine. Do the user's name and email address show up in the table? Could you show your AngularJS test function as well? Commented Dec 22, 2015 at 12:24
  • alert comes with undefined : app.controller('cntr',function($scope , $http){ $scope.test=function(obj){ alert(obj);} }); Commented Dec 23, 2015 at 5:05

2 Answers 2

5

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 %>)"
Sign up to request clarification or add additional context in comments.

Comments

-1

You can send any data via rendering templates.

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

1 Comment

This does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.