DEV Community

Danilo Alves
Danilo Alves

Posted on

How *ngIf teached me a lesson

Hi guys! This is my first post here on, and will be a short one. I just want to tell you a story about how *ngIf, an Angular's directive, teached me a lesson today.


I was trying to help my team to solve a problem were we had a modal and some nested components inside it, and some of these elements were formularys. I can't show the exact code, but here's a generic example:

<modal [visible]="isVisible" [properties]="someProperties">
Enter fullscreen mode Exit fullscreen mode

Everything was working just as it should, we could fill formulary and navigate between steps without losing data. But, when we closed modal, we found out that, if we open modal again, the data was still there, even when modal was closed!

The journey

We couldn't just pass an empty object to formulary's model when modal closes, it would be too complex to mantain, for other reasons. I began to search for alternatives, and I spent almost the whole afternoon trying to find a way to solve this problem. I tried a lot of things: use ViewChild decorator to watch modal and destroy its instance, use OnDestroy lifecycle to pass an empty object to every formulary when modal was closing... None of them worked.
I reached almost the end of my working journey without a solution, I was frustated and disappointed, thinking that this problem should be very simple to solve and that I wasn't capable to help my team.

The lesson

Then, I had an idea: "what if I use ngIf on steps component, associating with modal's visibility?". It will not work, my pessimist mind thought. I tried a whole lot of complex implementations and no one work, why would a ngIf go?
It's very easy to find out what happened, right? It worked just as I needed.
It's probably very situational, and wouldn't work for others as it worked for me, but it teached me a lesson that I'll probably carry for a time. Never subestimate the power of a simple solution. The basis can take you really far, even if your solutions aren't the prettiest or most performatic ones.

Top comments (0)