【发布时间】:2021-03-31 13:52:52
【问题描述】:
我正在尝试以角度创建自定义工具提示指令,我想将 templateRef 作为输入属性从父组件传递给该指令,但 templateRef.elementRef 返回的是注释而不是模板内容。
指令:
@Directive({
selector: '[tooltip]'
})
export class tooltipDirective {
@Input('tooltip') tooltip: ElementRef<any>;
@Input('placement') placement: string = 'bottom';
constructor(private el: ElementRef, private renderer: Renderer2) {
console.log(this.tooltip);
}
}
父组件:
<div [tooltip]="tooltipTemplate"> show tooltip </div>
<ng-template #tooltipTemplate>
<div class="tooltip">
tooltip text
</div>
</ng-template>
如何获取实际内容?
【问题讨论】:
-
不要使用构造函数(永远……真的)……试试 AfterViewInit 钩子……
-
@MikeOne,Afterviewinit 也给出了相同的结果
-
啊是的..对不起。我错过了您使用 Input 而不是 viewChild .. 说实话不知道如何传递..
-
好的没问题,感谢您的回复
标签: angular