ngIf with Angular 2 and TypeScript

The ngIf directive adds and removes elements in the DOM based on the results of an expression.

Updated Jan 2017, Angular version 2.4.x

First Example

The ngIf directive highlighted in this code will add or remove the div element depending on the expression in quotes.

ng-if.component.ts
import { Component } from '@angular/core';

@Component({
    selector: 'ng-if-directive',
    template: `
        <h2>ngIf</h2>
        <button (click)="show=!show">{{show ? 'hide' : 'show'}}</button>
        
<div *ngIf="show"> <p> <label><input #big type="checkbox" name="size" (change)="0" checked>Big</label> <label><input #small type="checkbox" name="size" (change)="0">Small</label> </p> <h3 *ngIf="big.checked">Big heading</h3> <p *ngIf="small.checked">Small paragraph</p> </div>`
}) export class NgIfComponent { private show = false; }

Second Example

These ngIf directives will add or remove the <h3> and <p> elements depending on the checked properties of the big and small reference variables.

ng-if.component.ts
import { Component } from '@angular/core';

@Component({
    selector: 'ng-if-directive',
    template: `
        <h2>ngIf</h2>
        <button (click)="show=!show">{{show ? 'hide' : 'show'}}</button>
        
        <div *ngIf="show">
            <p>
                <label><input #big type="checkbox" name="size" (change)="0" checked>Big</label>
                <label><input #small type="checkbox" name="size" (change)="0">Small</label>
            </p>
        
            <h3 *ngIf="big.checked">Big heading</h3>
            <p *ngIf="small.checked">Small paragraph</p>
        </div>`
})
export class NgIfComponent {
    private show = false;
}

Where Next?

To find out more about Angular and TypeScript, check out these tutorials.

  • Hello World - Implement a super-simple <hello-world> custom element using an Angular and TypeScript.
  • The Angular with TypeScript Tutorial - includes examples of components, template syntax, property binding, event binding, bootstrapping and more.
  • Configuration - Configure Angular and TypeScript to download dependencies from node modules or a CDN, and to compile the TypeScript during development or in the browser at runtime.
  • Templates - introduction to inline and external templates.
  • Interpolation - use curly braces and template expressions to output data on the page.
  • Property Binding - bind to DOM properties using square brackets and template expressions.
  • Event Binding - handle DOM events using parentheses and template statements.
  • Two-way Binding - combine property and event binding to create two-way binding with ngModel.
  • Input Binding - bind to <input> fields such as text, textarea, checkbox, radio and select.
  • Built-in Directives - see how to use built-in directives ngIf, ngSwitch, ngFor, ngClass and ngStyle.
  • Component Input Output - use @Input and @Output to pass data in to and out of a component.
  • Angular Router - Use the Angular router to navigate between components when the user clicks a link.
  • Nested Child Routes - An example of how child routes allow navigation between multiple views when a user clicks a link in a sub-menu.