What is the difference between ng-if and *ng-if in angular2

ShellZero picture ShellZero · Jul 8, 2016 · Viewed 20.6k times · Source

I'm trying to understand the difference between ng-if and *ng-if, but they look the same to me.

Is there a difference that I should keep in mind choosing one over the other and when to use these directives?

Answer

Thierry Templier picture Thierry Templier · Jul 8, 2016

ngIf is the directive. Because it's a structural directive (template-based), you need to use the * prefix to use it into templates.

*ngIf corresponds to the shortcut for the following syntax ("syntactic sugar"):

<template [ngIf]="condition">
  <p>
    Our heroes are true!
  </p>
</template>

Equivalent to:

<p *ngIf="condition">
  Our heroes are true!
</p>

See this doc for more details: