問題描述
我想使用我動(dòng)態(tài)添加的多個(gè) IonicSlides.但我不能使用 @viewChild
.請?zhí)岢鼋鉀Q此問題的方法.
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);
}
錯(cuò)誤:
_this.slides.toArray 不是函數(shù)
_this.slides.toArray is not a function
推薦答案
使用 @ViewChildren
代替 @ViewChild
, 閱讀更多
Use @ViewChildren
instead of @ViewChild
, Read More
@ViewChild:
您可以使用 ViewChild 來獲取第一個(gè)元素或指令匹配視圖 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.
這篇關(guān)于將 ViewChild 用于動(dòng)態(tài)元素 - Angular 2 &離子2的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!