Member-only story

Why & When You Should Use ngIf else

Tek Loon
1 min readMay 12, 2020

--

Photo by Adi Goldstein on Unsplash

Until a while ago, I still couldn’t convince myself when I should use Angular If Else Directives.

This is due to most of the time, writing 2 *ngIf (refer the below code) was enough to support my use case.

My 2 *ngIf validation

The ngIf else way

Why ngIf else?

After several rounds of thinking, I finally able to discover the reasons that are enough to persuade myself to use ngIf else.

  • Firstly, using ngIf else allows you to reduce one if validation. You don't have to write two *ngIf validation.
  • Secondly, you can reuse the loadingView declared above code. For e.g, you can reuse the loadingView when you're retrieving data from server or submitting data to server.

Originally published at https://tekloon.dev.

--

--

Tek Loon
Tek Loon

Written by Tek Loon

Coder and Writer. If you enjoy my stories— support me by https://www.buymeacoffee.com/tekloon so I can keep writing articles for the community.

No responses yet