鍍金池/ 問(wèn)答/HTML5/ angular5如何獲取一段html代碼賦值到另一個(gè)地方,html里面的(cli

angular5如何獲取一段html代碼賦值到另一個(gè)地方,html里面的(click)事件還要能生效?

Angular5剛剛接觸,很多東西不是太熟悉。
我想實(shí)現(xiàn)點(diǎn)擊Table的某一行,在點(diǎn)擊行的下方動(dòng)態(tài)插入一段HTML代碼,但是插入后,原來(lái)的點(diǎn)擊事件消失了,各位大神有沒(méi)有什么好的解決方案,

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;
  }
回答
編輯回答
短嘆

不評(píng)價(jià)你的代碼,但是你需要從 jq 思想轉(zhuǎn)變過(guò)來(lái)。動(dòng)態(tài)加上的html是使用不了angular的指令或事件的。

最簡(jiǎn)單的思路,用過(guò)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>

    <!--動(dòng)態(tài)顯示-->
    <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>
//你的組件類(lèi)
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
    }
}
2018年8月1日 13:05