angularjs输出纯文本,而不是HTML的 [英] angularjs to output plain text instead of html

查看:275
本文介绍了angularjs输出纯文本,而不是HTML的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文字是这样的:

 <跨度>我的文字< / SPAN>

我要显示无标签:

 我的文字

我也不想应用的标签,我想带他们。什么是简单的方法来做到这一点?

角HTML:

 < D​​IV> {{会将myText | htmlToPlaintext}}< / DIV>


解决方案

jQuery是约40倍慢,请不要使用jQuery对于简单的任务。

 函数htmlToPlaintext(文本){
  返回文本的?字符串(文本).replace(/< ^>] +> /克,''):'';
}

用法:

  VAR plain_text = htmlToPlaintext(your_html);


随着angular.js:

  angular.module('myApp.filters',[])。
  过滤器('htmlToPlaintext',函数(){
    复位功能(文本){
      返回文本的?字符串(文本).replace(/< ^>] +> /克,''):'';
    };
  }
);

使用:

 < D​​IV> {{会将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 );


With angular.js :

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

use :

<div>{{myText | htmlToPlaintext}}</div>  

这篇关于angularjs输出纯文本,而不是HTML的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆