没有其他html的angularjs换行符过滤器 [英] angularjs newline filter with no other html

查看:131
本文介绍了没有其他html的angularjs换行符过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将换行符( \\\
)转换为html br 的。 >
根据 Google群组中的讨论,以下是我():

  myApp.filter('newlines',function(){
return function(text){
return text.replace(/ \\\
/ g,'< br />');
}
});

这里的讨论还建议在视图中使用以下内容:

  {{dataFromModel |换行符| html}} 

这似乎是使用旧的 html filter,而现在我们应该使用 ng-bind-html 属性。




$无论如何,这会带来一个问题:我不希望任何来自原始字符串( dataFromModel )的HTML呈现为HTML;只有 br 的。



例如,给定以下字符串:


虽然7> 5

我仍​​然不希望html& ...

我想让它输出:

 虽然7& gt; 5< br>我仍然不希望html& amp; amp; amp; amp; amp; amp; amp;在这里的东西... 

有没有办法做到这一点?

解决方案


$ b

  App.filter('newlines',function(){
return function(text){
return text.replace(/ \\\
/ g,'< br / >');
}
))
.filter('noHTML',function(){
返回函数(文本){
返回文本
.replace(/& / g,'&')
.replace(/> / g,'& gt;')
.replace(/< / g, '& lt;');
}
});

然后,在我看来,我将其中一个输入到另一个中:

 < span ng-bind-html-unsafe =dataFromModel | noHTML | newlines>< / span> 


I'm trying to convert newline characters (\n) to html br's.
As per this discussion in the Google Group, here's what I've got:

myApp.filter('newlines', function () {
    return function(text) {
        return text.replace(/\n/g, '<br/>');
    }
});

The discussion there also advises to use the following in the view:

{{ dataFromModel | newline | html }}

This seems to be using the old html filter, whereas now we're supposed to use the ng-bind-html attribute.


Regardless, this poses a problem: I don't want any HTML from the original string (dataFromModel) to be rendered as HTML; only the br's.

For example, given the following string:

While 7 > 5
I still don't want html & stuff in here...

I'd want it to output:

While 7 &gt; 5<br>I still don't want html &amp; stuff in here...

Is there any way to accomplish this?

解决方案

Instead of messing with new directives, I decided to just use 2 filters:

App.filter('newlines', function () {
    return function(text) {
        return text.replace(/\n/g, '<br/>');
    }
})
.filter('noHTML', function () {
    return function(text) {
        return text
                .replace(/&/g, '&amp;')
                .replace(/>/g, '&gt;')
                .replace(/</g, '&lt;');
    }
});

Then, in my view, I pipe one into the other:

<span ng-bind-html-unsafe="dataFromModel | noHTML | newlines"></span>

这篇关于没有其他html的angularjs换行符过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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