javascript 将 ViewChild 用于动态元素 - Angular 2 & ionic 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48562310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:06:36  来源:igfitidea点击:

Use ViewChild for dynamic elements - Angular 2 & ionic 2

javascriptangularionic2viewchild

提问by Mohsen

I want use multiple IonicSlidesthat I added thease dynamically, So I can't use viewChild . please suggest a way for this problem.

我想使用动态添加的多个IonicSlides,所以我不能使用 viewChild 。请提出解决此问题的方法。

Template.html :

模板.html:

<div *ngFor="let name of title;let i = index;">

    <ion-slides  id="something" #slides  >
        //some code
    </ion-slides>
</div

Component.ts :

组件.ts:

  @ViewChild('slides') slides: QueryList<Slides>;

  ....

  ngAfterViewInit(){



        setTimeout( ()=>{

         alert(this.slides.toArray()); //this line rise error 


        },3000);


  }

Error :

错误 :

_this.slides.toArray is not a function

_this.slides.toArray 不是函数

回答by Vivek Doshi

Use @ViewChildreninstead of @ViewChild, Read More

使用@ViewChildren代替@ViewChild阅读更多

@ViewChild:

You can use ViewChild to get the first element or the directive matching the selector from the view DOM.

@ViewChildren:

You can use ViewChildren to get the QueryList of elements or directives from the view DOM.

@ViewChild

您可以使用 ViewChild 从视图 DOM 中获取与选择器匹配的第一个元素或指令。

@ViewChildren

您可以使用 ViewChildren 从视图 DOM 中获取元素或指令的 QueryList。