mdbootstrap / mdb-angular-ui-kit

Angular 18 & Bootstrap 5 & Material Design UI KIT
https://mdbootstrap.com/docs/angular/
Other
1.12k stars 280 forks source link

How to use tool tip with custom html template and also be able to show/hide them in code ? #129

Closed GurpreetSingh5 closed 5 years ago

GurpreetSingh5 commented 5 years ago

Expected behavior

Ability to use custom HTML, but also be able to manually hide/show tool-tip through component ts code So something like the combination of these two : image and image

Actual behavior

Not able to use these two together. There is no directive with "exportAs" set to "mdbTooltip"

Working environment and MDB version information

OS : Windows 10 MDB Version : 8.1.1

idzark commented 5 years ago

You need to use ViewChild decorator to get access to the tooltip methods. Here is an example:

HTML:

<ng-template #popTemplate> <div [innerHtml]="html"></div></ng-template>
<button
  #tooltip
  type="button"
  mdbBtn
  color="primary"
  class="waves-light"
  [mdbTooltip]="popTemplate"
  mdbWavesEffect
>
  Tooltip with html
</button>

<button mdbBtn color="primary" (click)="open()">Open tooltip</button>
<button mdbBtn color="secondary" (click)="close()">Close tooltip</button>

TS:

import { Component, ViewChild } from '@angular/core';
import { TooltipDirective } from 'angular-bootstrap-md';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  @ViewChild('tooltip', { static: true, read: TooltipDirective }) tooltip: TooltipDirective;
  html = '<span><i>Tooltip</i> <u>with</u> <b>HTML</b></span>';

  open() {
    this.tooltip.show();
  }

  close() {
    this.tooltip.hide();
  }
}
GurpreetSingh5 commented 5 years ago

Yes. This seems to work. Thanks. Closing this issue.

GurpreetSingh5 commented 5 years ago

There are other approaches too though. There is one another approach that worked. HTML:

      <ng-template #popTemplate>
      <div [innerHtml]="html"></div>
      </ng-template>
      <button
         #tooltip="mdb-tooltip"
         type="button"
         mdbBtn
         color="primary"
         class="waves-light"
         [mdbTooltip]="popTemplate"
         mdbWavesEffect
       >
         Tooltip with html
      </button>

TS:

import { Component, ViewChild } from '@angular/core';
import { TooltipDirective } from 'angular-bootstrap-md';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  html = `<span><i>Tooltip</i> <u>with</u> <b>HTML</b></span>
<button mdbBtn color="primary" (click)="tooltip.show()">Open tooltip</button>
<button mdbBtn color="secondary" (click)="tooltip.hide()">Close tooltip</button>`;

}