This is a problem with HTML. Not Angular. Angular records exactly what you told him.
In HTML, outside the <pre> , infinite spaces (returns, tabs, spaces, etc.) are treated as one empty space. That's why you need things like <br/> and if you are not using the <pre> tag block.
So try this in HTML so you understand what is going on:
<h3>Plain DIV</h3> <div> Some text here </div> <h3>Now with non-breaking spaces and line breaks</h3> <div> Some<br/> text<br/> here<br/> </div> <h3>Now with a PRE tag</h3> <pre> Some text here </pre>
Here is the scenario demonstrating above.
You will need to style the PRE tag with some custom class so that it looks like different text on your page, although most of the default styles for <pre> will use single-bit (fixed) width fonts such as Courier or Consolas.
It's either that you have to write a filter in Angular that handles spaces, tabs and line breaks in the correct markup .... which will be a pain to maintain.
EDIT: with the understanding above, Angular's solution (if you are not just using PRE)
Thus, the best solution, IMO, if you do not want to use the <pre> , would be to create a filter that will clean the text for you and process it with line breaks and inextricable spaces.
Something like that
app.filter('formatText', function (){ return function(input) { if(!input) return input; var output = input
Then, to use it, you would do something like this:
<span ng-bind-html="foo | formatText"></span>
It is important to note:
- You need to include the
angular-sanitize.js file in your script links. - In the module declaration, you need
'ngSanitize' :
var app = angular.module('myApp', ['ngSanitize']);
.. this means you can use the ng-bind-html or ng-bind-html-unsafe directives.
Below is a snippet showing usage.