angularjs to output plain text instead of html

JavascriptHtmlAngularjs

Javascript Problem Overview


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>

Javascript Solutions


Solution 1 - Javascript

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>  

Solution 2 - Javascript

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>

Solution 3 - Javascript

var app = angular.module('myapp', []);

app.filter('htmlToPlaintext', function()
{
    return function(text)
    {
        return  text ? String(text).replace(/<[^>]+>/gm, '') : '';
    };
});

<p>{{DetailblogList.description | htmlToPlaintext}}</p>

Solution 4 - Javascript

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.

Solution 5 - Javascript

<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

Solution 6 - Javascript

You can use ng-bind-html, don't forget to inject $sanitize service into your module Hope it helps

Solution 7 - Javascript

Use ng-bind-html this is only proper and simplest way

Solution 8 - Javascript

Use this function like

 String.prototype.text=function(){
   return this ? String(this).replace(/<[^>]+>/gm, '') : '';
 }
 
  "<span>My text</span>".text()
  output:
  My text

Fiddle

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarryView Question on Stackoverflow
Solution 1 - JavascriptUtopikView Answer on Stackoverflow
Solution 2 - JavascriptDavy RView Answer on Stackoverflow
Solution 3 - JavascriptKhyati AcharyaView Answer on Stackoverflow
Solution 4 - JavascriptOlivier CView Answer on Stackoverflow
Solution 5 - JavascriptPrasannaView Answer on Stackoverflow
Solution 6 - JavascriptHung VuView Answer on Stackoverflow
Solution 7 - JavascriptSandip JanaView Answer on Stackoverflow
Solution 8 - JavascriptpixelbyajView Answer on Stackoverflow