Click here to Skip to main content
15,886,788 members
Articles / Web Development / HTML5

Escape or Sanitize HTML in Angular

Rate me:
Please Sign up or sign in to vote.
4.11/5 (3 votes)
7 Jan 2016CPOL1 min read 23.5K   1  
Escape or sanitize HTML in Angular

Want to render some user-provided data to the page, but for whatever reason, you can’t use Angular’s {{ curly brace syntax }}?

Maybe your first thought is to mash it together with a string, like this:

C#
var content = "<span>" + userContent + "</span>";
element.text(content);

But be careful! This kind of thing can open you up to XSS attacks and other nasty things.

You need to escape or sanitize that data before placing it on the page.

Sanitize

Angular automatically sanitizes data if you use the ng-bind-html directive. This means it strips out HTML entirely.

A string of Hello <em>World!</em> becomes Hello World.

Manually Sanitize

If you want to sanitize data without using ng-bind-html, you can use the $sanitize service. Install it and require the module as a dependency:

bower install angular-sanitize

angular.module('yourThing', ['ngSanitize', ...])

Then, just inject it where needed, and use it:

JavaScript
function yourThing($sanitize) {
  return {
    function getContent(str) {
      return "<span>" + $sanitize(str) + "</span>";
    }
  };
}

Escape

Angular automatically escapes data if you use ng-bind or the {{ curly brace syntax }}. This means it outputs the literal characters instead of interpreting them as HTML.

Data that looks like Hello <em>World!</em> will render as Hello <em>World!</em> (not Hello World).

Manually Escape

Angular doesn’t seem to expose a built-in service for escaping. However Lodash (v3) has a _.escape function that does exactly this.

Install Lodash if you aren’t already using it:

bower install lodash

Then, escape your string as needed:

JavaScript
function yourThing() {
  return {
    function getContent(str) {
      return "<span>" + _.escape(str) + "</span>";
    }
  };
}

And with that, you and your users are safe for another day.

If you want my regularly-scheduled posts on best-practices Angular, as well as the all-new world of Angular 2, sign up for my newsletter below.

Thanks for reading!

Escape or Sanitize HTML in Angular was originally published by Dave Ceddia at Angularity on December 02, 2015.

This article was originally posted at https://daveceddia.com/feed.xml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Dave is a Software Engineer in the Boston area and writes about AngularJS and other JavaScript things over at daveceddia.com

Comments and Discussions

 
-- There are no messages in this forum --