Skip to content

How to reserve whitespace in an Angular application

Last updated on February 1, 2021

In this blog post, we will be going to learn about how to reserve the whitespace character in an angular application.

Yes, Probably this kind of situation we may face at least once in a while as a developer.

How to reserve whitespace?

By default, the angular framework does the whitespace character removal from the text nodes from both the ends.

We have 2 different options where each one fits into different needs.

  • ngPreserveWhitespaces
    • Using the above-said directive as an attribute, you could easily manage the whitespace within the DOM subtree like below,
<div ngPreserveWhitespaces>
   You can 
           easily manage
                 number of Whitespaces
</div>
  • &ngsp;
    • In the middle of the text nodes if we want to preserve any amount of whitespace we can use this.

If you know any other options, you can mention in the comments section.

Published inAngularFrameworksJavaScript