I have a string with a lot of extra whitespace, as it appears on my server:
var care_description = "MATERIAL\r\n \r\n 56% Acrylic, 24% Rayon, 20% Polyester\r\n \r\n CARE\r\n \r\n Machine Wash, Gentle Or Delicate"
Using the new Angular 1.2.0
ngBindHtml, I'm processing it in my controller like this:
$scope.care = $sce.trustAsHtml(care_description);
(the $scope.records[i].accordions
array is just a wrapper for Angular-Bootstrap's Accordions module).
However, when I display it in my view (using
<p ng-bind-html="care"></p>
), it shows up as:
MATERIAL 56% Acrylic, 24% Rayon, 20% Polyester CARE Machine Wash, Gentle Or Delicate
When it should actually be:
MATERIAL
56% Acrylic, 24% Rayon, 20% Polyester
CARE
Machine Wash, Gentle Or Delicate
One possible solution is to use a regex replace to find all instances of \r\n
and replace them with <br />
.
Edit: It's worth noting that even though there are no HTML
tags in the example above, there are often some, which is why I need to use ngBindHtml
instead of <pre>
tags.