【问题标题】:Button click showing same data in angular2按钮单击在 angular2 中显示相同的数据
【发布时间】:2017-02-16 11:44:41
【问题描述】:

我试图用表中各行的详细信息填充引导程序modal。模态显示很好,但它在点击任何按钮时填充只有第一行数据

这是一个链接:https://plnkr.co/edit/3tl6S9CuGCv8VxXsOq7Y?p=preview

我所做的是property binded组件的pass@Input装饰器中的data值,您可以在下面的代码中使用(请参阅plunker以了解有关组件的更多信息):

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Email</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor='let data of datas'>
        <td>{{data.name}}</td>
        <td>{{data.age}}</td>
        <td>{{data.email}}</td>
        <td><detail-emp [pass]='data'></detail-emp></td>
      </tr>
    </tbody>
  </table>
  </div>

现在我相信,当我将 datas 中的 datas 中的单个 data 传递给它的组件时,应该传递相应的行数据。但事实并非如此。单击任何模态按钮的模态仅显示第一行数据。

如何确保只有被点击行的数据显示在模态中,而不总是显示第一行的数据。

提前致谢。

【问题讨论】:

    标签: twitter-bootstrap angular bootstrap-table ngfor angular-components


    【解决方案1】:

    试试这个:

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" [attr.data-target]="'#exampleModal_' + pass.id ">
      Show Data
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal_{{pass.id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Employee Detail</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Name : {{pass.name}}</p>
            <p>Age : {{pass.age}}</p>
            <p>Email : {{pass.email}}</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    

    问题出在按钮和模式的 id 中。您需要为每行中的每个模式设置不同的 ID (exampleModal_{{pass.id}}) ..然后附加每个生成的 ID 的按钮 ( [attr.data-target]="'#exampleModal_' + pass.id ") ..你总是打开相同的模式..希望它有帮助

    【讨论】:

    • 效果很好。但它是如何工作的?我之前也看到它在任何按钮的点击时只显示第一行的 id。
    • 因为如果你没有为每一行的每个模式设置不同的 id .. 按钮总是只打开 id="exampleModal" 的模式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-06-14
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多