Tamás Polgár
Tamás Polgár

Reputation: 2262

Unicode characters in Angular binding

There are some answers on how to display Unicode character codes in Angular bindings, but I'm trying to display one dynamically and everything seems to fail.

I have a component which receives a character code as an @Input parameter.

<my-component [icon]="e901"></my-component>

The component then attempts to show it, but neither of these work:

<i>{{ "\u" + icon  }}</i>
<i [innerHTML]="'\u' + icon"</i>
<i [innerHTML]="`\u${icon}`"</i>
<i [innerHTML]="'&#' + icon + ';'"</i>

There's a lot of room to play with the syntax but it always either results in an error or just plain displays \ue901 as a string. It does work if I hardcode the code though:

<i>{{ "\ue901" }}</i>

It doesn't matter if I generate the string in the template or TypeScript. It doesn't work if I try to pass the entire code as a parameter. Any ideas, guys?

Upvotes: 4

Views: 16205

Answers (4)

Gvs Akhil
Gvs Akhil

Reputation: 2581

.ts file

icon = "\u{1F601}";

.html file

<p [innerHTML]="icon"></p>

Upvotes: 1

PatricioS
PatricioS

Reputation: 111

You can also use HTML code, here you can search it https://www.rapidtables.com/web/html/html-codes.html

Hope helps

Upvotes: 1

Tam&#225;s Polg&#225;r
Tam&#225;s Polg&#225;r

Reputation: 2262

After JGFMK pointed out half of the answer, here is the solution:

In the code:

this.icon = '&#x' + this.icon + ';';

Then in the template:

<span [innerHTML]="icon"></span>

Upvotes: 9

ahbon
ahbon

Reputation: 512

Actually it's not possible because you must access pseudo elements with Angular for what you want to achieve.

Maybe this answer will give you some hints.

Upvotes: -4

Related Questions