Reputation: 19913
In my mobile app using angular5/ionic3 app I want to show emoji icon.
So I used something like
<span>😁</span>
But in rendering it will show it as empty square.
I already have <meta charset="UTF-8">
in my index.html
and html file is also saved as utf-8.
I thought it could be related to sanitizing, so I created below pipe
@Pipe({
name: 'safeHtml',
})
export class SafeHtmlPipe implements PipeTransform {
constructor(private domSanitizer: DomSanitizer) { }
transform(html) {
return this.domSanitizer.bypassSecurityTrustHtml(html);
}
}
and then I used it as
<span [innerHTML]="'😁' | safeHtml"></span>
but still result is the same
What did I miss?
Upvotes: 6
Views: 11904
Reputation: 7276
This is usually happening when you don't have a font to display the character. On my computer Chrome uses the Symbola font to render this character. You can download it from here. Or you can find another font that supports emojis. Than you add the font in CSS using @font-face
@font-face {
font-family: "Symbola";
src: url("/fonts/Symbola-Emoji.woff") format("woff"),
url("/fonts/Symbola-Emoji.ttf") format("ttf");
}
Then you can use this font on a CSS class
.
.emoji {
font-family: "Symbola"
}
Then you add that class to your span
:
<span class="emoji">😁</span>
UPDATE:
If you assign 😁
to a variable and then read it inside the template it works fine:
<span [innerHTML]="someVariable | safeHtml"></span><!--this will work-->
But your code breaks: the U+1F601
character (a.k.a. 😁
) becomes U+F601
and since you don't have a font to display it you get an empty square. It looks like a bug in Angular's template rendering engine because even if I do <span [innerHTML]="(someVariable + '😁') | safeHtml"></span>
it still breaks. I get the 😁
and the U+F601
character. So this is not the sanitizer problem.
Upvotes: 4