【问题标题】:Get access to div where attribute angular component is placed访问放置属性角度组件的 div
【发布时间】:2021-08-11 07:40:27
【问题描述】:
我有一个像这样定义的角度分量
<div [app-collapsible-column] class="col-sm-3 relatives-left-list" [direction]="'left'">
我想要实现的是从 app-collapsible-column 组件代码中修改类。我知道我可以通过ViewChild 获得模板的引用。但是,如何获取组件本身定义的 div 的引用?
【问题讨论】:
标签:
angular
components
angular-components
【解决方案1】:
我们可以在 app-collapsible-column 组件中暴露ElementRef
constructor(public ele:ElementRef){
}
然后在父组件内部,我们可以使用 ViewChild 访问 div 元素,如下所示
@ViewChild(CollapsibleColumn) componentRef:CollapsibleColumn;
ngAfterViewInit(){
console.log(this.componentRef.ele);
}