Home > Net >  Angular - Get text from button to show different content in modal
Angular - Get text from button to show different content in modal

Time:07-15

I'm currently learning Angular and Typescript, I don't know much yet

I have a table which shows data from an array of objects with ngFor

<tbody *ngFor="let book of tableBooklist">
  <tr>
    <td>{{book.title}}</td>
    <td>
      <button (click)="toggleModal()" >{{book.author.nameAut   ' '   book.author.surnameAut}}</button>
    </td>
    <td>{{book.publicationYear}}</td>
    <td>{{book.publisher}}</td>
    <td>{{book.genre}}</td>
    <td>{{book.numberOfCopies}}</td>
    <td>{{book.firstPublication}}</td>
  </tr>
</tbody>

The button triggers a modal component. I need to read the name of the button I clicked in order to show different content on my modal. For example, I click on the button with the name "Lev Tolstoj" and the modal shows the details from the author (that I have in my array).

I don't know how to get the content from the button, since I print it with ngFor.

CodePudding user response:

Pass the book object as parameter.

 <td>
  <button (click)="toggleModal(book)" >{{book.author.nameAut   ' '   book.author.surnameAut}}</button>
</td>

You can access the book object and pass it to your modal

public toggleModal(book: Book){
     //code to pass book (book.author) to your modal
}
  • Related