angular5如何获取一段html代码赋值到另一个地方,html里面的(click)事件还要能生效?

发布于 2022-09-07 07:58:54 字数 2141 浏览 17 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2022-09-14 07:58:54

不评价你的代码,但是你需要从 jq 思想转变过来。动态加上的html是使用不了angular的指令或事件的。

最简单的思路,用过ngIf控制:

<!--你的html-->
...
<ng-container *ngFor="let item of tableLists;let ind = index">
    <tr class="alarm-info" (click)="onClickDetailPanel(ind)">
        <th scope="row">{{name}}</th>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
    </tr>

    <!--动态显示-->
    <tr class="alarm-detail-panel" *ngIf="item.showTemp">
        <th colspan="4">
            <div id="collapseL1" class="collapse" data-parent="#collapseOne">
                <div class="card-body">
                    ...
                </div>
            </div>
        </th>
    </tr>
</ng-container>
//你的组件类
export class XXComponent{
    let tableLists:any[] = [
        { name:1,showTemp:false },
        { name:2,showTemp:false },
        { name:3,showTemp:false },
        { name:4,showTemp:false },
        { name:5,showTemp:false }
    ]
    ...
    onClickDetailPanel(index:number){
        this.tableLists[index].showTemp = !this.tableLists[index].showTemp
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文