angular5如何获取一段html代码赋值到另一个地方,html里面的(click)事件还要能生效?
Angular5刚刚接触,很多东西不是太熟悉。
我想实现点击Table的某一行,在点击行的下方动态插入一段HTML代码,但是插入后,原来的点击事件消失了,各位大神有没有什么好的解决方案,
HTML代码如下:
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of [1, 2, 3, 4, 5]" class="alarm-info" (click)="onClickDetailPanel($event, '1')" data-toggle="collapse" data-target="#collapseL1" aria-expanded="false" aria-controls="collapseL1">
<th scope="row">{{item}}</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
</tbody>
</table>
ts代码如下:
onClickDetailPanel(e, params) {
console.log("detail panel," + e + ", params:" + params);
$(".alarm-detail-panel").remove();
var tempHTML = `
<tr class="alarm-detail-panel">
<th colspan="4">
<div id="collapseL1" class="collapse" data-parent="#collapseOne">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</th>
</tr>
`;
e.currentTarget.outerHTML += tempHTML;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不评价你的代码,但是你需要从 jq 思想转变过来。动态加上的html是使用不了angular的指令或事件的。
最简单的思路,用过ngIf控制: