How to implement a directive that adds tooltips to form controls dynamically

0 votes
Can Someone help me regarding How to implement a directive that adds tooltips to form controls dynamically?
2 days ago in Angular by Nidhi
• 14,600 points
19 views

1 answer to this question.

0 votes

Custom Tooltip Directive

1. Create the Directive

import { Directive, Input, ElementRef, Renderer2, HostListener } from '@angular/core';

@Directive({

  selector: '[appTooltip]'

})

export class TooltipDirective {

  @Input('appTooltip') tooltipText: string = '';

  private tooltipElement: HTMLElement;

  constructor(private el: ElementRef, private renderer: Renderer2) {}

  @HostListener('mouseenter') onMouseEnter() {

    this.tooltipElement = this.renderer.createElement('span');

    this.renderer.appendChild(

      this.tooltipElement,

      this.renderer.createText(this.tooltipText)

    );

    this.renderer.addClass(this.tooltipElement, 'tooltip');

    this.renderer.setStyle(this.tooltipElement, 'position', 'absolute');

    this.renderer.setStyle(this.tooltipElement, 'background', '#333');

    this.renderer.setStyle(this.tooltipElement, 'color', '#fff');

    this.renderer.setStyle(this.tooltipElement, 'padding', '4px 8px');

    this.renderer.setStyle(this.tooltipElement, 'borderRadius', '4px');

    this.renderer.setStyle(this.tooltipElement, 'top', `${this.el.nativeElement.offsetTop - 30}px`);

    this.renderer.setStyle(this.tooltipElement, 'left', `${this.el.nativeElement.offsetLeft}px`);

    this.renderer.appendChild(document.body, this.tooltipElement);

  }

  @HostListener('mouseleave') onMouseLeave() {

    if (this.tooltipElement) {

      this.renderer.removeChild(document.body, this.tooltipElement);

      this.tooltipElement = null;

    }

  }

}

2. Usage in Template

<input type="text" appTooltip="Enter your full name" />

answered 2 days ago by anonymous

Related Questions In Angular

0 votes
1 answer

How to implement a directive that manages focus on dynamically added elements?

To implement a directive that manages focus ...READ MORE

answered 2 days ago in Angular by anonymous
32 views
0 votes
1 answer

How to build a directive that listens for specific keyboard shortcuts?

You can use the @HostListener decorator to ...READ MORE

answered 2 days ago in Angular by anonymous
42 views
0 votes
1 answer
0 votes
1 answer

How to do a Jquery Callback after form submit?

Hello @kartik, Just do like this - $("#myform").bind('ajax:complete', function() ...READ MORE

answered Jul 24, 2020 in Angular by Niroj
• 82,840 points
8,416 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
107 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to develop a directive that modifies element styles based on form validation status?

Here's a concise directive that automatically applies ...READ MORE

answered 2 days ago in Angular by anonymous
21 views
0 votes
1 answer

How to create a custom directive that integrates with external charting libraries?

Compact Directive for External Chart Integration import { ...READ MORE

answered 2 days ago in Angular by anonymous
22 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP