Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  161] [ 2]  / answers: 1 / hits: 7120  / 3 Years ago, wed, march 17, 2021, 12:00:00

I am using https://ng-bootstrap.github.io/#/components/tooltip/ and required to open tooltip on mouse over and also on click.


Basically, on mouse hover text1 appears and on click tooltip text changes to text2. In examples I see only on one event ( either on click or hover).


best example is - https://pypi.org/project/nltk/ - same functionality I needed using https://ng-bootstrap.github.io/#/components/tooltip/.


Angular 7 is being used in my project.


More From » tooltip

 Answers
5

You can use the context and manual triggers functionality of ngb-tooltip to achieve this.


In your HTML, define the tooltip:


<ng-template #tipContent let-greeting="greeting">{{greeting}}</ng-template>

The above HTML will display the value of the variable greeting as the text in the tooltip.


Also define the button (or other HTML) element that you want the tooltip to appear on


<button
type="button"
class="btn btn-outline-secondary mr-2"
[ngbTooltip]="tipContent"
triggers="manual"
#t1="ngbTooltip"
(mouseover)="openTooltip(t1, 'text 1')"
(mouseout)="closeTooltip(t1)"
(click)="openTooltip(t1, 'text 2')">
Tooltip on top
</button>

This uses manual triggers, and you can bind to the mouseover and mouseout events to display the tooltip text 1 on hovering over the button. The click binding will display the tooltip text 2 when clicking on the button.


The openTooltip function is defined in the TypeScript file opens the tooltip passing in the text that was passed in as an argument to the method. The line tooltip.close() is required otherwise the clicking on the button to display the text 2 tooltip will not work:


openTooltip(tooltip, greeting: string) {
tooltip.close();
tooltip.open({ greeting });
}

There's a working StackBlitz demo here.


The closeTooltip function simply closes the tooltip:


closeTooltip(tooltip) {
tooltip.close();
}

[#1639] Wednesday, March 10, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;