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

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

Angular5剛剛接觸,很多東西不是太熟悉。
我想實現(xiàn)點擊Table的某一行,在點擊行的下方動態(tài)插入一段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;
  }
回答
編輯回答
短嘆

不評價你的代碼,但是你需要從 jq 思想轉(zhuǎn)變過來。動態(tài)加上的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>

    <!--動態(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>
//你的組件類
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