0
<div *ngFor="let task of data ; let i = index" [attr.data-index]="i">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>
  <div class="modal fade" id="{{task.id}}">
    //HTMl Content
  </div>
</div>

In the above code I am unable to add the data-target Dynamically for the button. i have gone through this but this is modifying for all the modals at once only and having same name.

Thanks for your Help in advance.

1 Answer 1

1

You'd have to use [attr.data-target]="'#MyModal'+task.id" ... considering the data structure is:

this.data = [
       {id:1, buttonLabel: 'test label 1', modalBody: 'Demo Modal body contents #1' }
      ,{id:2, buttonLabel: 'test label 2', modalBody: 'Demo Modal body contents #2' }
      ,{id:3, buttonLabel: 'test label 3', modalBody: 'Demo Modal body contents #3' }
      ];

This would be the HTML:

<div *ngFor="let task of data ; let i = index" [attr.data-index]="i">
  <button type="button" class="btn btn-primary" data-toggle="modal" [attr.data-target]="'#MyModal'+task.id">
    Open modal
  </button>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

this is the demo link: stackblitz.com/edit/…

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.