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>
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 );
angular.module('myApp.filters', []).
filter('htmlToPlaintext', function() {
return function(text) {
return text ? String(text).replace(/<[^>]+>/gm, '') : '';
};
}
);
use :
<div>{{myText | htmlToPlaintext}}</div>
return text ? String(text).replace(/<[^>]+>/gm, '') : "";
–
Josefajosefina /<.+?>/gm,
which looks a bit more intuitive, readable and also saves you a few characters. –
Valida "<p><span style="color: rgb(34, 36, 38);text-align: left;float: none;background-color: rgb(255, 255, 255);">Some Text.</span><!--EndFragment--><br/><br/><br/></p>"
–
Malevolent Some Text.
, as I would expect it –
Congruent <img src="file>.jpg" width="640" height="400" alt="image"><p><a href="http://example.com/path?var=>&utm_campaign=123">Hello</a><!-- sun > world -->World</p>
–
Jun 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>
angular.element('<div>'+text+'</div>').text();
–
Incoming text
is not already wrapped in some HTML tag. So to be robust it should be more like: return angular.element('<div>' + text + '</div>')
–
Talanian var app = angular.module('myapp', []);
app.filter('htmlToPlaintext', function()
{
return function(text)
{
return text ? String(text).replace(/<[^>]+>/gm, '') : '';
};
});
<p>{{DetailblogList.description | htmlToPlaintext}}</p>
You want to use the built-in browser HTML strip for that instead of applying yourself a regexp. It is more secure since the ever green browser does the work for you.
angular.module('myApp.filters', []).
filter('htmlToPlaintext', function() {
return function(text) {
return stripHtml(text);
};
}
);
var stripHtml = (function () {
var tmpEl = $document[0].createElement("DIV");
function strip(html) {
if (!html) {
return "";
}
tmpEl.innerHTML = html;
return tmpEl.textContent || tmpEl.innerText || "";
}
return strip;
}());
The reason for wrapping it in an self-executing function is for reusing the element creation.
<div ng-bind-html="myText"></div>
No need to put into html {{}} interpolation tags like you did {{myText}}.
and don't forget to use ngSanitize in module like e.g.
var app = angular.module("myApp", ['ngSanitize']);
and add its cdn dependency in index.html page https://cdnjs.com/libraries/angular-sanitize
You can use ng-bind-html, don't forget to inject $sanitize service into your module Hope it helps
Use ng-bind-html this is only proper and simplest way
Use this function like
String.prototype.text=function(){
return this ? String(this).replace(/<[^>]+>/gm, '') : '';
}
"<span>My text</span>".text()
output:
My text
© 2022 - 2024 — McMap. All rights reserved.