我有一個案例,我決定使用需要訪問 ElementRef 的指令,以便通過 Renderer 操作 dom。
此時,由于 Direcive 放置在 *ngFor 回圈下的 HTML 元素上,當指令呼叫 elemenRef.nativeElement 時,它會得到一整套它們(顯然,每次 *ngFor 迭代都有一個)。
有沒有辦法按 ID 或任何型別的選擇過濾 nativeElements?
謝謝
uj5u.com熱心網友回復:
使用渲染器你可以試試這個:
在您的 html 檔案中:
<div *ngFor="let element of elements">
<div [attr.id]="element.id"></div>
</div>
這將為 *ngFor 回圈中的每個元素提供一個 id。然后在你的 ts 檔案中:
let element = this.renderer.selectRootElement(`#${id}`);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/515366.html
標籤:有角度的单页应用角度指令
