按钮单击显示角度2中的相同数据
问题描述:
我试图使用表中各行的详细信息填充引导程序modal
。莫代尔显示正常,但它是越来越填充只有第一行数据点击任何按钮。按钮单击显示角度2中的相同数据
这里有一个链接: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>
现在我beleive是,当我在*ngFor
从datas
通过个人data
其成分,各行数据应该传递。但事实并非如此。点击任何模式按钮的模式只显示第一行数据。
如何确保只有来自点击行的数据显示在模式中,而不是始终是第一行的数据。
在此先感谢。
答
试试这个:
<!-- 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">×</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}})每次模态中的每一行。 ([attr.data-target] =''#exampleModal_'+ pass.id“) ..你总是打开相同的模式..hope它有帮助
工作很安静好。但它是如何工作的?我之前也看到,它只是在任何按钮的点击上显示第一行的id。 –
因为如果你没有为每一行的每个模态设置不同的ID ..按钮总是只打开模式id =“exampleModal” –
非常感谢。它解决了问题。 –