iT邦幫忙

1

[已解決]如何拿到*ngFor還未生成的DOM呢?

  • 分享至 

  • twitterImage

如題...
這是一個廣告輪播
想說拖曳其中一則廣告其他的廣告也會一起移動的話
就把被touchmove的廣告的移動值套在其他的廣告上, 讓他們能一起動....

如何在*ngFor還未生成的DOM上設定參考變數呢?

 <div class="container">
    <img  class="film"  *ngFor='let img of this.film' >
   </div>

或是可以拿到這些DOM的方法....??

DanSnow iT邦好手 1 級 ‧ 2021-05-20 08:21:11 檢舉
我不是寫 Angular 的我也不知道,不過建議你附上為什麼你需要 DOM 的原因,說不定也有不用 DOM 就可以解決的方法
感恩 已修改內容
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
黃升煌 Mike
iT邦研究生 5 級 ‧ 2021-05-20 09:45:37
最佳解答

可以用 樣板參考變數 + @ViewChildren 來達到你要的效果

https://angular.io/api/core/ViewChildren

HTML:

<div class="container">
  <img #filmImage class="film" *ngFor='let img of this.film' >
</div>

TS:

@ViewChildren('filmImage') filmImages: QueryList<ElementRef>;

ngAfterViewInit() {
  console.log(this.films.length);
}

感恩! thats what i need

我要發表回答

立即登入回答