参见英文答案 >
How can I select an element in a component template?10个
我已经创建了一个自定义组件,我将其置于for循环中,例如
我已经创建了一个自定义组件,我将其置于for循环中,例如
<div *ngFor="let view of views"> <customcomponent></customcomponent> </div>
其输出将是:
<customcomponent></customcomponent> <customcomponent></customcomponent> <customcomponent></customcomponent>
我想知道当这些组件的数量可以变化时,如何使用@viewchild语法或任何其他方法来获取对这些组件的引用
<customcomponent #compID></customcomponent>
我可以参考如下:
@ViewChild('compID') test: CustomComponent
如果不是这种情况,例如使用索引,我该如何引用它?
(此问题与使用ElementRef无关,因为之前已经提到的其他问题可以通过下面列出的答案看到)此问题涉及访问多个@ViewChild和使用列表查询.
使用@ angular / core中的@ViewChildren来获取对组件的引用
模板
<div *ngFor="let v of views"> <customcomponent #cmp></customcomponent> </div>
零件
import { ViewChildren,QueryList } from '@angular/core'; /** Get handle on cmp tags in the template */ @ViewChildren('cmp') components:QueryList<CustomComponent>; ngAfterViewInit(){ // print array of CustomComponent objects console.log(this.components._results); }
原文地址:https://www.jb51.cc/angularjs/143586.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。