Angular: Binding an HTML value

Posted Over 9 years ago. Visible to the public.

To bind an HTML value to ng-bind-html, you need to mark it as "trusted" first. Among other ways, you can do so with a custom filter.

# Filter in Coffeescript...:
@app.filter 'htmlSafe', ['$sce', ($sce) -> $sce.trustAsHtml ]

# ...or JS:
app.filter('htmlSafe', [
  '$sce', function($sce) {
    return $sce.trustAsHtml;
  }
]);

# Usage:
<div ng-bind-html="item.value | htmlSafe"></div>

This is a replacement for the ng-bind-html-unsafe directive which has been removed in Angular 1.2.

:party:

Thomas Klemm
Last edit
Over 9 years ago
Thomas Klemm
License
Source code in this card is licensed under the MIT License.
Posted by Thomas Klemm to makandra dev (2014-11-13 18:04)