What is the best way to catch and format the "\n\n" inside of text being passed from the server to display line breaks? Fiddle is here: http://jsfiddle.net/nicktest2222/2vYBn/
$scope.data = [{
terms: 'You agree to be bound be the terms of this site. \n\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tempus lectus ac nunc malesuada, fringilla feugiat nibh rhoncus. Vestibulum adipiscing mi in est consectetur, vitae facilisis nulla tristique. Nam eu ante egestas, ultricies tellus eu, suscipit neque.\n\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum et ligula non tellus semper iaculis eget vestibulum metus. Nunc aliquam eros sit amet sapien posuere, ac hendrerit risus ultricies. Vivamus nec enim sed eros placerat pulvinar a quis dui.',
agreed: false
}];
You can also use a custom filter to replace \n
to <br>
.
<p ng-bind-html-unsafe="data[0].terms | nl2br"></p>
And the filter.
angular.module('myApp', [])
.filter('nl2br', function(){
return function(text) {
return text ? text.replace(/\n/g, '<br>') : '';
};
});
** EDIT/UPDATE - 2014-12-10 **
Since new versions of Angular removed ng-bind-html-unsafe
@Tamlyn answer is now the best answer. I just changed the way $sce
got injected into the function for speed purpose.
HTML
<p ng-bind-html="data[0].terms | nl2br"></p>
JS
.filter('nl2br', ['$sce', function ($sce) {
return function (text) {
return text ? $sce.trustAsHtml(text.replace(/\n/g, '<br/>')) : '';
};
}]);
jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With