有没有办法找回ViewChildren
or ContentChildren
按元素类别?
这可以通过 id 或组件工作,但不适用于基于类的查询,即classedViewItems
and classedContentItems
@Component({
selector: 'my-container',
template '<div><ng-content><ng-content></div>'
})
export class MyContainer {
@ViewChildren('item') viewItems: QueryList;
@ContentChildren(MyItem) contentItems: QueryList;
@ViewChildren('.fine-me') classedViewItems: QueryList; // <-- need this to work
@ContentChildren('.find-me') classedContentItems: QueryList; // <-- or this
}
对于以下情况:
<my-container>
<my-item class="find-me" #item *ngFor="let item; of items"></my-item>
</my-container>
我需要通过元素类获取查询列表而不对其进行修饰。
@ViewChild()
, @ViewChildren()
, @ContentChild()
, @ContentChildren()
仅支持模板变量的名称(或逗号分隔的名称列表)或组件或指令的类型作为选择器,正如角度文档中也提到的那样。
没有办法使用其他选择器。
你能做的就是过滤QueryList
之后只获取具有特定类的元素,但这并不能让您免于向每个元素添加模板变量。
也可以看看如何选择组件模板中的元素? https://stackoverflow.com/questions/32693061/angular-2-typescript-get-hold-of-an-element-in-the-template/35209681#35209681
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)