admin管理员组文章数量:1134247
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 > 5<br>I still don't want html & stuff in here...
Is there any way to accomplish this?
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 > 5<br>I still don't want html & stuff in here...
Is there any way to accomplish this?
Share Improve this question asked Dec 20, 2012 at 3:12 MegaHitMegaHit 2,6444 gold badges26 silver badges28 bronze badges7 Answers
Reset to default 283Maybe you can achieve this only with html, a <preformated text>
way ? It will avoid from using filters or do any kind of processing.
All you have to do is display the text within an element that has this CSS:
<p style="white-space: pre;">{{ MyMultiLineText}}</p>
This will parse and display \n as new lines. Works great for me.
Here, a jsFiddle example.
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, '&')
.replace(/>/g, '>')
.replace(/</g, '<');
}
});
Then, in my view, I pipe one into the other:
<span ng-bind-html-unsafe="dataFromModel | noHTML | newlines"></span>
A simpler way to do this is to make a filter that splits the text at each \n
into a list, and then to use `ng-repeat.
The filter:
App.filter('newlines', function() {
return function(text) {
return text.split(/\n/g);
};
});
and in the html:
<span ng-repeat="line in (text | newlines) track by $index">
<p> {{line}}</p>
<br>
</span>
If you do not want to destroy the layout with endless strings, use pre-line:
<p style="white-space: pre-line;">{{ MyMultiLineText}}</p>
I'm not aware if Angular has a service to strip html, but it seems you need to remove html before passing your newlines
custom filter. The way I would do it is through a custom no-html
directive, which would be passed a scope property and the name of a filter to apply after removing the html
<div no-html="data" post-filter="newlines"></div>
Here's the implementation
app.directive('noHtml', function($filter){
return function(scope, element, attrs){
var html = scope[attrs.noHtml];
var text = angular.element("<div>").html(html).text();
// post filter
var filter = attrs.postFilter;
var result = $filter(filter)(text);
// apending html
element.html(result);
};
});
The important bit is the text
variable. Here I create an intermediate DOM element and append it the HTML using the html
method and then retrieve only the text with the text
method. Both methods are provided by Angular's lite version of jQuery.
The following part is applying the newline
filter, which is done using the $filter
service.
Check the plunker here: http://plnkr.co/edit/SEtHH5eUgFEtC92Czq7T?p=preview
An update to the filter with ng-bind-html currently would be:
myApp.filter('newlines', function () {
return function(text) {
return text.replace(/( )? /g, '<br/>');
}
});
and the noHTML filter is no longer required.
white-space solution is having low browser support: http://caniuse.com/#search=tab-size
Bit late to the party on this but I would suggest a small improvement to check for undefined / null strings.
Something like:
.filter('newlines', function () {
return function(text) {
return (text) ? text.replace(/( )? /g, '<br/>') : text;
};
})
Or (bit tighter)
.filter('newlines', function () {
return function(text) {
return (text instanceof String || typeof text === "string") ? text.replace(/( )? /g, '<br/>') : text;
};
})
本文标签: javascriptangularjs newline filter with no other htmlStack Overflow
版权声明:本文标题:javascript - angularjs newline filter with no other html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736796996a1953325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论