Issue with data-title attribute in tooltips not functioning properly with Angular 6 and ngx-translate

Currently, I am working on an Angular 6 application that utilizes ngx-translate for localization. Additionally, I have implemented Bootstrap 4 tooltips into the project. However, I am encountering a challenge where I am unable to maintain both localization and the Bootstrap tooltip style simultaneously.

In the absence of localization, I would typically utilize an input field in the following manner:

<input type="text" class="form-control text-truncate" id="position"
placeholder="position" data-title="position" />

This approach displays a visually appealing Bootstrap tooltip, as shown below:

https://i.sstatic.net/BM69n.png

However, when incorporating ngx-translate for localization, my input field is structured as follows:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position" 
    [attr.placeholder]="'wfrh_vacancyform_position' | translate" 
    [attr.data-title]="'wfrh_vacancyform_position' | translate" />

The issue arises with the "data-title" attribute, which seems to encounter difficulties with ngx-translate integration. While the placeholder translates correctly, the tooltip fails to display.

I have also attempted another method:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position"
placeholder="{{'wfrh_vacancyform_position' | translate}}" 
data-title="{{'wfrh_vacancyform_position' | translate}}" />

Unfortunately, this approach only works for the placeholder and not for the tooltip. Subsequently, I tried:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position" 
    [attr.placeholder]="'wfrh_vacancyform_position' | translate" 
    [attr.title]="'wfrh_vacancyform_position' | translate" />

When implemented this way, the tooltip appears but lacks styling, as depicted in the image below:

https://i.sstatic.net/x1G6S.png

Moreover, the code snippet illustrates how I handle tooltips within the code (in ngOnInit):

ngOnInit() {
   setTooltip()
}

  setTooltip() {

    $('.tooltipped').tooltip({
      placement: 'auto',
      trigger: 'focus',
      template: '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner bg-dark text-light"></div></div>'
    });

    $('.tooltipped').bind("mouseover", function () {
      var _this = $(this);
      _this.tooltip("show");
    });

    $('.tooltipped').bind("mouseout", function () {
      var _this = $(this);
      _this.tooltip("hide");
    });

    $('.tooltipped').bind("keyup", function () {
      var _this = $(this);
      _this.tooltip("hide");
    });
  }

At this point, I find myself at an impasse. I seek assistance in achieving a properly styled tooltip with translation functionality. Any suggestions or guidance would be greatly appreciated.

Thank you.

Answer №1

After extensive research, I finally discovered a solution to the issue at hand. I want to share it here in case it may benefit others.

The problem stemmed from setting the tooltip during the onInit event, which only occurs once when the component is initially created. I neglected to specify tooltip text and relied on the following code snippet to handle translation:

[attr.data-title]="'text_to_translate_key' | translate"

As a result, when changing languages, the tooltip did not refresh as expected, displaying static content based on the initial value. By utilizing a function with the tooltip "title" property, we can overcome this issue:

    $('.tooltipped').tooltip({
      placement: 'auto',
      trigger: 'focus',
      template: '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner bg-dark text-light"></div></div>',
      title: this.updateTitle
    });

  updateTitle() {

    return $(this).attr("placeholder");
  }

This function, with the current object reference (this), serves as a binding mechanism that continuously updates the title property. As a result, changes to the placeholder text (which does not require refreshing after language changes) will automatically update the tooltip's text for an enhanced user experience.

"That's all for now!"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: "An issue has been encountered within Angular 4 where it is

Thank you for any assistance, I realize this may be a beginner question... but I seem to be missing something and my TypeScript code is error-free. My goal is simple: I want to track which Main Menu Item is currently selected. To achieve this, I have bou ...

Ways to access the value of an input field using Javascript

I am currently utilizing the valums-file-uploader plugin for file uploads via ajax. However, I have encountered an issue with its functionality. Below is the script that I am using: <input type="text" id="Gaurav" name="Gaurav" /> <script src="fil ...

The clear fix HTML code doesn't seem to be functioning properly in Google Chrome

Recently, I crafted this basic HTML: Within this code are 2 SPAN elements. I intended for one to sit beneath the other by clearing the float. The bbb element is styled with float:left. To handle the layout on the aaa element, I utilized Facebook's ...

Is it possible to assign numerical values to attributes in HTML code?

I'm unsure about something - is it possible to set an attribute value as a number? For example: <div data-check="1"></div> Is this the correct way to do it or not? I've heard conflicting opinions, with some people saying you shouldn ...

How can I customize the styling of Angular Material Datepicker?

I am currently incorporating the Angular Material Datepicker into various parts of my application. One particularly important usage is a legacy one that must remain untouched for the proper functioning of the application. This legacy Datepicker is extensiv ...

The EventEmitter in Angular 8 is prohibiting the emission of an Object

Is there a way I can emit an Object instead of primitive data types? I have an object const amount = { currenty: 'USD', total: '10.25' }; that I need to emit to the parent component. export class MyChildComponent implements OnInit { ...

Maintain the background color of the form select drop down even after clicking away from the form

I have customized a dropdown menu. <select name="country"> <option value="Andorra">Andorra</option> <option value="Albania">Albania</option> <option value="Armenia">Armenia</option> <opt ...

Is there a way to set up a background process within electron that captures a screenshot of the entire desktop?

I'm currently working on a Desktop application using Angular2 and Electron that captures screenshots of the entire desktop and saves them to a specified path on my PC. The code for capturing screenshots is implemented in the app.component.ts file, but ...

Issue with Android date input field not resetting the value

When testing in Android Tablet (version 4.1.2), I encountered a strange issue with an input date field that looks like this: <input type="date" value="2013-06-10"/>. Upon clicking on the field and trying to set a new date, instead of replacing the o ...

Issue encountered during the construction of the Angular project in production

$ ng build --prod Date: 2018-12-06T18:43:56.689Z Hash: e36e17503416de0fc128 Time: 7480ms chunk {0} runtime.ec2944dd8b20ec099bf3.js (runtime) 1.44 kB [entry] [rendered] chunk {1} main.9868d9b237c3a48c54da.js (main) 128 bytes [initial] [rendered] chunk {2} ...

Arrow indicating the correct direction to expand or collapse all items with a single click

I have successfully implemented the "expand/collapse all" function, but I am facing an issue with the arrow direction. The arrows are not pointing in the correct direction as desired. Since I am unsure how to fix this problem, I have left it empty in my co ...

Applying a CSS border to a div that perfectly encloses a span, adjusting to the

My goal is to create a nested structure where a div contains a span element like this: ---------- |Sentence| ---------- ----------------- |It's a looooong| |sentence | ----------------- While it works well for short sentences, long ones end u ...

Querying PHP to explore a sub-directory in search of a particular file

I've been searching for a solution to my issue. My website contains numerous files in various sub-directories within one main directory. I require a search functionality that can look for part of a file name and return the result, regardless of case s ...

The CLI variation is exclusively designed to work with Angular versions starting from 14.0.0 up

I encountered the following issue: This CLI version is designed to work with Angular versions ^14.0.0, however, I have Angular version 15.0.0 installed. For instructions on updating Angular, please visit: https://update.angular.io/ Even after updating An ...

How do I ensure that my font size completely fills the space in a justified layout using CSS?

Is there a way to style my text to match the appearance in the image shown using CSS? ...

Error: Angular version 5.1.0 is unable to set the header content-type to application/json for HttpClient

I have been attempting to customize the header for a POST API request to be of content type application.json. let options: { headers?: {'Content-Type':'application/json'} } Unfortunately, this customization is not being successfully a ...

Utilize the HTML canvas to sketch on an image that has been inserted

I have a HTML canvas element within my Ionic application. <canvas id="canvas" color="{{ color }}" width="800" height="600" style="position:relative;"></canvas> Within this canvas, I am loading an image. Below is the code snippet from the con ...

The Google calendar integration is working, but the appearance leaves much to

I embedded a Google calendar, but I'm having trouble centering it on the page without it overflowing onto the top menu. Can you help me fix this issue? If you'd like to see what I'm referring to, you can visit this link: ...

The responsive navigation bar yielded an unforeseen outcome

Looking to create a responsive navigation bar similar to the one shown here My code successfully shows and hides the navigation items, but it's not updating the burger icon No console errors or warnings are present This is my HTML: <nav> ...

The function 'find' cannot be invoked on an undefined object

I'm currently working on implementing objects in my jQuery code. So far, I have the following: var options = { ul: $(this).find('.carousel'), li: options.ul.find('li') } The li property is causing an error - Cannot call meth ...