AngularJS: newline characters to paragraph elements
The best solution I could think of was to create a filter:
angular.module('myApp').
filter('nlToArray', function() {
return function(text) {
return text.split('\n');
};
});
This takes a block of text and creates a new array element for each paragraph.
This array can then be plugged into an ng-repeat directive:
<p ng-repeat="paragraph in textBlock|nlToArray track by $index">{{paragraph}}</p>
var myApp = angular.module('myApp', ['ngSanitize']);
myApp.controller('myCtrl', function($scope){
$scope.myText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\nSed diam nonummy nibh euismod tincidunt ut laoreet dolore.\nMagna aliquam erat volutpat. Ut wisi enim ad minim veniam."
});
myApp.filter('nl2p', function () {
return function(text){
text = String(text).trim();
return (text.length > 0 ? '<p>' + text.replace(/[\r\n]+/g, '</p><p>') + '</p>' : null);
}
});
http://jsfiddle.net/moderndegree/934aZ/
You might solve this with css property and use {{text}} into p html element
white-space: pre-line;