Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 807
Harry Avatar asked Jun 25 '13 05:06

Harry


People also ask

Does AngularJS use HTML?

AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.

Is angular written in HTML?

Each Angular template in your application is a section of HTML to include as a part of the page that the browser displays. An Angular HTML template renders a view, or user interface, in the browser, just like regular HTML, but with a lot more functionality.


2 Answers

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>   
like image 67
Utopik Avatar answered Oct 21 '22 10:10

Utopik


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> 
like image 23
Davy R Avatar answered Oct 21 '22 11:10

Davy R