Within an angular partial I am looping over a list of entries as follows:
<ul class="entries">
<li ng-repeat="entry in entries">
<strong>{{ entry.title }}</strong>
<p>{{ entry.content }}</p>
</li>
</ul>
the content of {{entry.content}} have some line breaks which are ignored by angular. how can i make it preserve the linebreaks ?
It is just basic html. AngularJs won't change anything about that. You could use a pre tag instead:
<pre>{{ entry.content }}</pre>
Or use css:
p .content {white-space: pre}
...
<p class='content'>{{ entry.content }}</p>
If entry.content contains html code, you could use ng-bind-html:
<p ng-bind-html="entry.content"></p>
Don't forget to include ngSanitize:
var myModule = angular.module('myModule', ['ngSanitize']);
I make filters
// filters js
myApp.filter("nl2br", function($filter) {
return function(data) {
if (!data) return data;
return data.replace(/\n\r?/g, '<br />');
};
});
then
// view
<div ng-bind-html="article.description | nl2br"></div>
I fix by add pre-line
<style>
pre{
white-space: pre-line;
}
</style>
My data:
<pre>{{feedback.Content}}<pre>
//angular filter
angular.module('app').filter('newline', function($sce) {
return function(text) {
text = text.replace(/\n/g, '<br />');
return $sce.trustAsHtml(text);
}
});
//html
<span ng-bind-html="someText | newline"></span>