angularjs to output plain text instead of html
I have some text like this:
<span>My text</span>
I want to display without tags:
My text
I also don't want to apply the tags, I want to strip them. What's an easy way to do that?
Angular html:
<div>{{myText | htmlToPlaintext}}</div>
jQuery is about 40 times SLOWER, please do not use jQuery for that simple task.
function htmlToPlaintext(text) {
return text ? String(text).replace(/<[^>]+>/gm, '') : '';
}
usage :
var plain_text = htmlToPlaintext( your_html );
With angular.js :
angular.module('myApp.filters', []).
filter('htmlToPlaintext', function() {
return function(text) {
return text ? String(text).replace(/<[^>]+>/gm, '') : '';
};
}
);
use :
<div>{{myText | htmlToPlaintext}}</div>
from https://docs.angularjs.org/api/ng/function/angular.element
angular.element
wraps a raw DOM element or HTML string as a jQuery element (If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite.")
So you simply could do:
angular.module('myApp.filters', []).
filter('htmlToPlaintext', function() {
return function(text) {
return angular.element(text).text();
}
}
);
Usage:
<div>{{myText | htmlToPlaintext}}</div>