Thursday, November 26, 2015

Angular JS : Escaping the html from the string

Angular JS : Escaping the html from the string

Is there an angular JS command that will do HTML escaping on text?

How to escape HTML from string in AngularJS ?

Need a filter which can be used to escape the html tags from the string.

Escaping HTML is different then sanitizing HTML.

No in build command to escape the HTML from string in angular.


angular.module('app').filter('EscapeHtml', function() {

var tokenMap = {
'&': '&',
'<': '<',
    '>': '>',
'"': '"',
'\'': ''',
'/': '/',
'`': '`',
'=': '='
};

return function(str) {
 return String(str).replace(/[&<>"'`=\/]/g, function(s) {
   return tokenMap[s];
  });
};
});