0

I am fetching my users in the form of a JSONArray and I want to show the data of each user when clicked on their names. but I am not able to pass the user.id into the function.

<ng-container *ngFor = "let user of users" >
        <button (onclick)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
      </ng-container>
3
  • Angular uses event names for binding. docs: angular.io/guide/event-binding#binding-to-events Commented Oct 7, 2021 at 6:55
  • 2
    Sidenote: you don't have to use ng-container you can put the *ngFor directive directly on your button element. Commented Oct 7, 2021 at 6:56
  • Too many answer can be found on the stackoverflow for similar questions. For example; stackoverflow.com/questions/40211730/… is related to button usage on angular. Commented Oct 7, 2021 at 7:15

3 Answers 3

3

Can you please try the same with using only click() instead on onclick()

<ng-container *ngFor = "let user of users" >
    <button (click)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
  </ng-container>
Sign up to request clarification or add additional context in comments.

Comments

2

Its angular use (click) instead of (onclick)

<ng-container *ngFor="let user of users">
  <button (click)="getdata(user.id)" mat-raised-button color="primary">
    {{ user.name }}
  </button>
</ng-container>
getdata(id) {
  console.log(id);
}

Comments

1

try this

<ng-container *ngFor = "let user of users" >
    <button (click)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
</ng-container>

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.