【问题标题】:How I can Dynamically Create Bootstrap Modal using For loop Angular6如何使用 For 循环 Angular6 动态创建引导模式
【发布时间】:2019-09-11 09:14:25
【问题描述】:
<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>

在上面的代码中,我无法为按钮动态添加数据目标。我已经通过this 但这只是一次修改所有模态并且具有相同的名称。

提前感谢您的帮助。

【问题讨论】:

    标签: angular bootstrap-4 bootstrap-modal


    【解决方案1】:

    你必须使用[attr.data-target]="'#MyModal'+task.id" ...考虑到数据结构是:

    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' }
          ];
    

    这将是 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>
    

    【讨论】:

    猜你喜欢
    • 2017-01-08
    • 2021-04-23
    • 2020-09-11
    • 1970-01-01
    • 2014-05-04
    • 2014-03-31
    • 1970-01-01
    • 2019-04-13
    • 2020-04-15
    相关资源
    最近更新 更多