Tutorial

New Features for ngIf Starting with Angular 4

Published on March 16, 2017
author

Alligator.io

New Features for ngIf Starting with Angular 4

Angular 4 is due to arrive in just a few days, and with it comes great additions to the built-in ngIf template directive. Namely, we can now define a template to use for an else clause, and we can assign a local variable that holds the resulting value of the if clause.

Here’s how to use these two new features for ngIf:

Else Clause

So it used to be, in order to have an else clause as part of an ngIf statement, you would have to instead create another ngIf statement with the negation of the first statement:

<div *ngIf="likeThis">
  Like this!
</div>
<div *ngIf="!likeThis">
  Like that!
</div>

But now we can define an else clause with the reference name for a template that we define with ng-template:

<div *ngIf="things.car; else noCar">
  Nice car!
</div>
<ng-template #noCar>
  Call a Uber.
</ng-template>

Local Variable Assignment

Often the resulting value of an if statement won’t just be the boolean true or false but will instead be a value that you may want to reference easily. With the new ngIf you can easily assign the result of the if statement to a local variable. For example, our things object looks like this in our component class:

things = {
  car: 'Honda',
  shoes: 'Nike',
  shirt: 'Tom Ford',
  watch: 'Timex'
};

And you can reference the result of the if statement like this:

<div *ngIf="things.car; let car">
  Nice {{ car }}!
</div>
<!-- Nice Honda! -->

You can of course combine both an else clause and a local variable assignment:

<div *ngIf="things.car; else noCar; let car">
  Nice {{ car }}!
</div>
<ng-template #noCar>
  Call a Uber.
</ng-template>

ngIf with Async Pipe

The new functionalities for the ngIf directive become very powerful when used in combination with the async pipe, as explained in this post from Angular University.

You can, for example, use the async pipe to automatically subscribe to an observable, define a loading template with the else clause and place the resulting value from the observable into a local variable.

Let’s create a simple observable to illustrate:

// ...
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/delay';

// ...
export class AppComponent {
  things$ = Observable.of({
    car: 'Honda',
    shoes: 'Nike',
    shirt: 'Tom Ford',
    watch: 'Timex'
  }).delay(1000);
}

Our observable mimics a network call by waiting for 1 second before emitting the data.

Now you can use the async pipe to subscribe to the things$ observable, define an else clause with a template for a loading message, and declare a local variable that you’ll be able to access once the observable gets its data:

<div *ngIf="things$ | async; else loading; let things">
  Nice {{ things.watch }}! <!-- Nice Timex! -->
</div>
<ng-template #loading>
  Loading your things...
</ng-template>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Alligator.io

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.