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;
  }
阅读 2.1k
1 个回答

不评价你的代码,但是你需要从 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
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进