Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display HTML special characters in Angular 2 bindings?

Tags:

angular

I have a normal binding like this {{foo}} and it displays foo's value as text in the HTML. The text that comes from the server is "R&D". I need this to display as "R&D". Any help?

like image 347
David Avatar asked Aug 24 '17 15:08

David


People also ask

Can I use HTML to display special characters?

When you want to insert a special character, select Insert > HTML > Special Characters. From there are you presented with a few of the most common, or you can choose “Other” to view all the characters available. Simply select the character you would like to insert and the code is inserted for you.

What is special character in HTML?

Special characters are specific pieces of HTML code designed to display characters that are used in the HTML code or to include characters that are not found on the keyboard in the text the viewer sees.


2 Answers

{{}} is for string binding.

Use attribute binding to innerHTML instead to get these characters displayed correctly.

<div [innerHTML]="foo"> 

See https://stackoverflow.com/a/41089093/217408 for more details.

like image 86
Günter Zöchbauer Avatar answered Sep 28 '22 15:09

Günter Zöchbauer


For a little more fun and flexibility, you can create a pipe that parses HTML entities:

@Pipe({name: "decodeHtmlString"}) export class DecodeHtmlString implements PipeTransform {     transform(value: string) {         const tempElement = document.createElement("div")         tempElement.innerHTML = value         return tempElement.innerText     } }  {{foo | decodeHtmlString}} 
like image 34
John Montgomery Avatar answered Sep 28 '22 13:09

John Montgomery