Skip to content

Instantly share code, notes, and snippets.

@montyr75
Last active May 31, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montyr75/71085efbb57117ca1b94d45286515d09 to your computer and use it in GitHub Desktop.
Save montyr75/71085efbb57117ca1b94d45286515d09 to your computer and use it in GitHub Desktop.
Angular 2 directive to safely inject HTML.
import 'dart:html' show Element, NodeTreeSanitizer;
import 'package:angular2/core.dart'
show Directive, Input, OnChanges, SimpleChange;
@Directive(selector: '[safeInnerHtml]')
class SafeInnerHtml implements OnChanges {
Element _el;
SafeInnerHtml(this._el);
@Input()
String safeInnerHtml;
@override
void ngOnChanges(Map<String, SimpleChange> changes) {
_el.setInnerHtml(this.safeInnerHtml, treeSanitizer: NodeTreeSanitizer.trusted);
}
}
<span [safeInnerHtml]="someHtml"></span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment