angularjs 输出普通文字 html

我有这样的文字:


<span>My text</span>


我想在没有标签的情况下显示:


My text


我也不想申请标签,我想删除它们。 什么是简单的方法?

Angular html:


<div>{{myText | htmlToPlaintext}}</div>
已邀请:

诸葛浮云

赞同来自:

http://jsperf.com/strip-html-jquery-vs-pure-js
, 请不要使用 jQuery 这个简单的任务。


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


使用 :


var plain_text = htmlToPlaintext/ your_html /;


从 angular.js :


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


使用 :


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

石油百科

赞同来自:

经过
https://docs.angularjs.org/api ... ement

angular.element

加工原料 DOM 或字符串 HTML 作为一个元素 jQuery /如果一个 jQuery
无法使用, angular.element 委托内置价值 Angular
子集 jQuery, 叫 "jQuery lite" 或者 "jqLite."/

所以你可能会做:


angular.module/'myApp.filters', []/.
filter/'htmlToPlaintext', function// {
return function/text/ {
return angular.element/text/.text//;
}
}
/;


使用:


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

江南孤鹜

赞同来自:

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

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

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

帅驴

赞同来自:

您想使用内置浏览器 HTML strip 为此,而不是应用正则表达式。 这是更安全的,因为绿色浏览器为您工作。


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;
}///;


在自我实现函数中包装它的原因是重用元素的创建。

卫东

赞同来自:

<div ng-bind-html="myText"></div>

不需要插入 html {{}} 插值标签,你是怎么做到的 {{myText}}.

并且不要忘记使用 ngSanitize 例如,在模块中
var app = angular.module/"myApp", ['ngSanitize']/;


并添加你的依赖 cdn 在 index.html p。
https://cdnjs.com/libraries/angular-sanitize

诸葛浮云

赞同来自:

您可以使用 ng-bind-html, 不要忘记为模块输入服务 $sanitize
我希望这个能帮上忙

裸奔

赞同来自:

使用 ng-bind-html 它只是正确和最简单的方法。

君笑尘

赞同来自:

使用此功能


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

"<span>My text</span>".text//
output:
My text


http://jsfiddle.net/zVMHK/32/

要回复问题请先登录注册