In html I have a div with a tooltip binded to a function.
Something like this:
<div *ngFor="let user of users">
<div class="myClass" [matTooltip]="user.name + '
' + getUserPhones(user.Id)" matTooltipClass="tooltip-large">{{ user.fullName }}</div>
</div>
This code here: [matTooltip]="user.name + ‘
‘ + getUserPhones(user.Id)" it is generating a tooltip witch will look like:
"UserName
–> User Phone 1
–> User Phone 2"
Typescript Code:
public getUserPhones(userId: string): string {
const currentUser = this.users.find((user) => user.userId === userId);
const userPhonesArray = viewArr.phoneNumbers;
const userPhonesDiplayText = '';
if (userPhonesArray .length <= 0) {
return '';
} else {
for (let i = 0; i < userPhonesArray.length; i++) {
userPhonesDiplayText += '→' + userPhonesArray[i] + '
' ;
return userPhonesDiplayText ;
}
}
}
What I am trying to do: In case a user has more than 1 phone numbers I want to display an arrow with the phone in new line
The issue is that the codes passed to html do not change to arrow(‘→’) and new line (‘
‘).
I am getting something like: Username
→ Phone1
→ Phone2
How can I pass the string from typescript to html and get the arrow and the empty line?
Is it possible?
2
Answers
To pass UTF-8 arrow character (→, represented as →) from TypeScript to HTML and display it, you can use JavaScript/TypeScript to manipulate the HTML content. Here’s how you can do it:
HTML File:
In your HTML file, create an element (e.g., a , , or any other HTML element) where you want to display the arrow character. Give it an id or class for easy selection.
TypeScript/JavaScript Code:
In your TypeScript or JavaScript code, you can access the HTML element by its id or class, and then set its innerHTML to the UTF-8 arrow character entity (→).
You can’t directly pass HTML character entities into the string, as they will be displayed as strings.
What you can is embed relevant Unicode characters in the string itself by using it’s codepoint.
The syntax is
uXXXX
oru{XXXX}
. The codepoint of right arrow is 2192, so you should be able to achieve it by addingu2192
to the string.Likewise, for line breaks, you can simply add
rn
to the string.So you would end up with something like
u2192 123456789 rn u2192 987654321
. This should correctly display both arrows and line breaks in the tooltip. Note that by default material tooltips don’t respect inserted linebreaks, so you’ll have to add custom styling to them, namelywhite-space: pre-line;
.You can do this by defining your class in global
styles.scss
files, e.g.And applying that class to your tooltip, e.g.: