Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Preserve Whitespace in Angular Templates

Angular removes whitespace-only nodes and collapses spaces by default. Choose a single-space entity, subtree attribute, component setting, or compiler option based on what must be preserved.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular removes whitespace-only text nodes between elements and collapses runs of whitespace inside ordinary text nodes by default. For one needed space, use &ngsp;; to preserve a particular subtree, add ngPreserveWhitespaces; and use preserveWhitespaces: true only when broader preservation is required. If the app uses hydration, Angular recommends leaving preservation disabled.

What Angular does to template whitespace

Angular templates are compiled, and the compiler simplifies whitespace by default. According to Angular’s Whitespace in templates guide and Component API, the default transformations include:

As an Amazon Associate I earn from qualifying purchases.

  • Removing whitespace-only text nodes between elements, such as indentation and line breaks between tags.
  • Collapsing a run of whitespace characters inside an ordinary text node to a single space.
  • Trimming whitespace at template boundaries.

This is a compile-time behavior; source indentation is not necessarily a text node in the resulting view. The API documentation says these transformations do not affect text nodes inside <pre> or <textarea>, where whitespace is significant.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the narrowest preservation option

Need Use Scope and effect
One space between text or elements &ngsp; Inserts a single space. Repeated instances still collapse to one space while preserveWhitespaces is false.
A sequence of spaces or whitespace in one subtree ngPreserveWhitespaces Preserves whitespace in the selected element’s subtree.
Whitespace throughout one component template preserveWhitespaces: true in @Component Preserves potentially superfluous whitespace in that component’s compiled template.
Broader preservation through compiler configuration Set the Angular compiler option preserveWhitespaces: true Applies more broadly; Angular’s compiler-options page lists the default as false.

These scopes and behaviors are documented in Angular’s Component API and compiler options reference.

Preserve only one required space

Use Angular’s &ngsp; entity where the space belongs. For example:

<p>Hello&ngsp;world</p>

The compiler turns the entity into one space. It is not a way to preserve a run of multiple spaces: when preserveWhitespaces remains false, repeated &ngsp; instances also collapse to one. For a sequence that must remain intact, preserve the containing subtree with ngPreserveWhitespaces.

Preserve whitespace in a subtree or component

Selected subtree

Add ngPreserveWhitespaces to the element containing the content whose whitespace matters. This limits the change to that subtree rather than preserving whitespace across unrelated templates.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Component template

Set preserveWhitespaces: true in the component metadata when whitespace must be retained throughout that component’s template:

@Component({
  selector: 'app-example',
  templateUrl: './example.html',
  preserveWhitespaces: true
})

Angular documents this option in the Component API. Prefer a scoped option if only part of the template needs it.

Compiler configuration

Set the Angular compiler option preserveWhitespaces: true when the requirement applies more broadly than one component. The Angular compiler options reference lists false as the default. A broader setting retains more whitespace than a local fix requires, so use it deliberately.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Hydration: keep configuration consistent

Angular recommends keeping preserveWhitespaces false when using hydration. Its compiler options reference and hydration guide warn that enabling preservation can cause hydration issues and advise consistent configuration between server and browser builds.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is a caution, not a claim that every app with whitespace preservation will fail. If a specific template requires it, keep the server and browser configurations aligned and validate the affected hydrated pages.

Why not preserve whitespace everywhere?

Angular’s template guide explains that retaining otherwise unnecessary whitespace creates extra text nodes and increases page-rendering overhead. The Component API also notes potential effects on DOM-node layout. These are qualitative cautions: the cited official documentation does not provide a numeric performance estimate.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.