0

Angular $sce service seems to be encoding characters and not trusting the html. Is there an option to have the html trusted?

$scope.text = $sce.trustAsHtml('it's broken')

An example.

<p>it&#39;s working</p>
<p>{{ text }}</p>

Looks like.

it's working
it&#39;s broken

I'd rather not use ng-bind-html because it's meant to be used in a filter like the following.

{{ text | render }}
AJcodez
  • 31,780
  • 20
  • 84
  • 118

1 Answers1

0

It is not the $sce that encode your html, actually nothing does that.

But when you use an interpolation {{ text }}, angular will detect that and replace it with a correct value via textNode.nodeValue, not something like innerHTML. Therefore, your &#39; will be treated as a normal text, not an encoded HTML entity.

That's why the ng-bind-html exists, and nothing prevent you from using the filter inside the ng-bind-html expression.

<div ng-bind-html="text | render | trustAsHtml"></div>

Example filters:

.filter('render', function () {
  return function (value) {
    return value + '!';
  };
})

.filter('trustAsHtml', function ($sce) {
  return function (value) {
    return $sce.trustAsHtml(value);
  };
})

Example Plunker: http://plnkr.co/edit/F8OQvoSzOR06TPepc2Fo?p=preview

runTarm
  • 11,537
  • 1
  • 37
  • 37