Is there an angular JS command that will do HTML escaping on text? I am processing a custom directive and have need to escape some of the output which it generates.
There are two ways to do HTML sanitization in AngularJS. The first is by using the ngBindHtml directive and the second by using the $sanitize service.
function MyCtrl ( $scope, $sanitize ) {
$scope.rawHtml = "";
$scope.sanitizedHmtl = $sanitize( $scope.rawHtml );
}
Then these two are functionally equivalent:
If used in a directive, as in your question, you can simply insert the sanitized HTML:
element.html( scope.sanitizedHtml );
But in most cases when writing directives, you'd have this in a template and use ngBindHtml, as above. But it works for the corner cases.