Using TemplateRef to create a tooltip/popover directive in Angular 2

转载 2016年08月30日 16:25:11

This post is about a Component created in the context of our application development.There is a demo here, and you can find the full source code here.

Lately, the need arose to create a tooltip directive. This brought up a lot of questions we hadn’t had to face before, such as how to create markup wrapping around rendered content, or rather“what is Angular 2’s transclude?”

Turns out, using TemplateRef is very useful for this, but the road to understanding it wasn’t easy. After seeing it used in a similar fashion by Ben Nadel, I decided to take a stab at it.

TemplateRef is used when using <template> elements, or perhaps most commonly when using *-prefixed directives such as NgFor of NgIf. For *-prefixed directives (or directives in <template>elements, TemplateRef can be injected straight into the constructor of the class. For other components, however, they can be queried via something like the ContentChild decorator.

Initially, I had thought to create two directives: a TooltipDirective to be placed on the parent element, plus a TooltipTemplate directive to be placed in a template, that would then inject itself into the parent. It proved too complex, though, and after finding what could be done with the ContentChild query the implementation became much simpler.

The end result looks like this (simplified for clarity):

    selector: "[tooltip]"
export class TooltipDirective implements OnInit {
    @Input("tooltip") private tooltipOptions: any;
    @ContentChild( "tooltipTemplate" ) private tooltipTemplate: TemplateRef <Object>;
    private tooltip: ComponentRef<Tooltip>;
    private tooltipId: string;
    constructor (
        private viewContainer: ViewContainerRef,
        public elementRef: ElementRef,
        private componentResolver: ComponentResolver,
        private position: PositionService ) {
            this.tooltipId = _.uniqueId("tooltip");
    ngOnInit () {
        // Attach relevant events
    private showTooltip () {
        if (this.tooltipTemplate) {
                .then(factory => {
                    this.tooltip = this.viewContainer.createComponent(factory);
                    this.tooltip.instance["content"] = this.tooltipTemplate;
                    this.tooltip.instance["parentEl"] = this.elementRef;
                    this.tooltip.instance["tooltipOptions"] = this.options;
    private hideTooltip () {
        this.tooltip = undefined;
    private get options (): TooltipOptions {
        return _.defaults({}, this.tooltipOptions || {}, defaultTooltipOptions);
    selector: "tooltip",
    `<div class="inner">
        <template [ngTemplateOutlet]="content"></template>
    <div class="arrow"></div>`
class Tooltip implements AfterViewInit {
    @Input() private content: TemplateRef <Object>;
    @Input() private parentEl: ElementRef;
    @Input() private tooltipOptions: TooltipOptions;
    constructor (
        private positionService: PositionService,
        public elementRef: ElementRef)
    private position() {
        // top and left calculated and set
    ngAfterViewInit(): void {

The TooltipDirective requires a <template #tooltipTemplate> element, that gets rendered through a  Tooltip Component, created and injected with the templateRef containing our content. Essentially, “transcluding” it. The Tooltip component’s role is only to wrap the content with some light markup, and position itself when inserted into the page.

A lot of the actual positioning (not shown here, but in the source code) is done directly to the rendered elements, though – I faced some issued when using the host properties object, that I believe were reintroduced in the latest RC.

All in all, it was a great learning experience, and Angular 2’s <template> surely beats Angular.js’transclude. Slowly but surely Angular 2 get’s more and more demystified to me, but it is hard work getting there.



pip Fatal error in launcher: Unable to create process using

接上篇“Eclipse启动报错:JVM terminated. Exit code=2”,今天把Python的安装位置也从C盘剪切到了D盘,然后修改了Path环境变量中对应的盘符:D:\Python2...

kubernetes1.4 基础篇:Learn Kubernetes 1.4 by 6 steps(3):Step 2. Using kubectl to Create a Deployment


How to Create and Optimize Sprite Sheets in Cocos2D with Texture Packer and Pixel Formats

How to Create and Optimize Sprite Sheets in Cocos2D with Texture Packer and Pixel FormatsLike this p...

How To Create Buttons in Cocos2D: Simple, Radio, and Toggle

How To Create Buttons in Cocos2D: Simple, Radio, and ToggleLike this post? Follow me on Twitter!Butt...

Using #region Directive With JavaScript Files in Visual Studio

本文转载自: 这是一个不错的小创意,可...


首先创建一个指令,采用@input方式,来获取jquery插件所需要的参数。 在ngOnChanges时,也就是参数通过@input传入时,初始化jquery插件, 初始化jquery插件需要获取do...


Directive 4/16/2017 4:40:14 PM what it does Marks a class as an Angular directive and collects di...