問題描述
我想使用我動態添加的多個 IonicSlides.但我不能使用 @viewChild
.請提出解決此問題的方法.
I want to use multiple IonicSlides that I added dynamically. But I can't use @viewChild
. please suggest a way to resolve this problem.
模板.html:
<div *ngFor="let name of title;let i = index;">
<ion-slides id="something" #slides>
//some code
</ion-slides>
</div
Component.ts:
@ViewChild('slides') slides: QueryList<Slides>;
....
ngAfterViewInit(){
setTimeout(()=>{
alert(this.slides.toArray()); //this line rise error
}, 3000);
}
錯誤:
_this.slides.toArray 不是函數
_this.slides.toArray is not a function
推薦答案
使用 @ViewChildren
代替 @ViewChild
, 閱讀更多
Use @ViewChildren
instead of @ViewChild
, Read More
@ViewChild:
您可以使用 ViewChild 來獲取第一個元素或指令匹配視圖 DOM 中的選擇器.
You can use ViewChild to get the first element or the directive matching the selector from the view DOM.
@ViewChildren:
您可以使用 ViewChildren 獲取元素的 QueryList 或來自視圖 DOM 的指令.
You can use ViewChildren to get the QueryList of elements or directives from the view DOM.
這篇關于將 ViewChild 用于動態元素 - Angular 2 &離子2的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!