0

This works: <div [innerHTML]="rssItem.title"></div>

This does not:
<input type="text" formControlName="title" [(ngModel)]="rssItem.title" name="title" #title class="form-control" [innerHTML]="rssItem.title">

Why?

I have attempted using a sanitize html pipe as explained here but it does not work for text input.

Using Angular2 version 2.4.5 on Windows.

Community
  • 1
  • 1
septemberbrain
  • 998
  • 9
  • 25

2 Answers2

2

Here is a simple way to decoding HTML characters (Reference)

constructor() {
   let str = "&#39134;&#24605;&#21345;&#23572;&#26368;&#26032;MCU";
   this.rssItem.title = this.decodeHtml(str);
}

private decodeHtml(html: string) {
 var txt = document.createElement("textarea");
 txt.innerHTML = html;
 return txt.value;
}

Furthermore, use Sanitizer to have the safe html string. (Reference)

Community
  • 1
  • 1
KarateJB
  • 941
  • 2
  • 10
  • 22
1

Found a solution based on this answer

Created a private function :

decodeEntities(encodedString) {
  var textArea = document.createElement('textarea');
  textArea.innerHTML = encodedString;
  return textArea.value;
}

And used it to decode the string.

Tried to create a pipe using the same code but wasn't able to get it to work in the Input Text tag. If someone has an idea on how to do that please post it.

Community
  • 1
  • 1
septemberbrain
  • 998
  • 9
  • 25