3

I am writing a html in where the html will display a list of records with three fields - id, name and an icon. When the icon will be clicked by user, a method will be called where the id will be passed as parameter and details of that record will be returned. The code snipped I am following is as below -

<tr *ngFor="let userDetail of userDetails">
                  <td>{{ userDetail.id }}</td>
                  <td>{{ userDetail.firstname }}  {{userDetail.lastname}}</td>
                  <td><a href="javascript:void(0);" (click) = "showUserDetails()" ><img src="/assets/user_details.png" width="20" height="20" /></a></td>
                </tr>

Here I need to pass the userDetail.id when the showUserDetails() function will be created. But still unable to pass the parameter. What will be best way to pass that parameter? Thanks in advance.

7
  • I don't know Angular from squat so I probably shouldn't even comment, but I can't stop myself!! Would it not be: (click) = "showUserDetails({{ userDetail.id }})" Commented Mar 13, 2020 at 11:22
  • 1
    Simply use showUserDetails(userDetail.id). Commented Mar 13, 2020 at 11:28
  • 1
    (click) = "showUserDetails(userDetail.id)" Commented Mar 13, 2020 at 11:28
  • Tried both of those options...but it didn't work... Commented Mar 13, 2020 at 11:28
  • 2
    codesandbox.io/s/amazing-sky-4jfb7 Commented Mar 13, 2020 at 11:34

1 Answer 1

4

You have to make sure:

  1. You are passing the value into the method:(click) = "showUserDetails(userDetails.id)"

  2. The method should be declared in the same component otherwise you have to bind e.g. the service it's implented in within a public variable in the components constructor.

I created a simple stackblitz project. that may can help you.

Sign up to request clarification or add additional context in comments.

Comments

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.