Running “Getting Started with Angular Signals” Code Lab Locally
After fixing local Angular build errors for the Getting Started with Angular Signals code lab sample application, I saw it was up and running but not running correctly. Given the fact I had to fix installation errors for this sample before fixing the build errors, seeing runtime errors were not a surprise.
Of course, some of the missing functionality is intentional, because this is a hands-on code lab where we fill in a few intentional gaps to see Angular signals in action. I've done the exercise once before on StackBlitz so I knew what to expect. The signal()
and computed()
exercises went smoothly, but the effect()
had no effect. Every time solvedMessage
signal changed, our effect()
was supposed to compare it against superSecretMessage
. And if they matched, launch a little confetti effect. I saw the completion confetti on StackBlitz, and nothing on my local version. Time to start debugging.
There were no error messages, so I started by adding a few console.log()
to see what is and isn't happening. It led me to the conclusion that the code inside my effect()
call was not getting executed. This is a bit of a pickle. Standard debugging procedure is to attach a breakpoint to an interesting piece of code and see what happens when it is called. But in this case, my problem is that the code was not getting called. D'oh! A breakpoint on code inside effect()
would be useless because it is never hit. In these situations, the next thing to try is setting a breakpoint somewhere upstream. Ideally in the bit of logic that decided whether to call my code or not. Unfortunately, here it means setting a breakpoint somewhere inside Angular's implementation of effect()
and I don't know where that is or where to even start looking. I barely understand code in an Angular application, digging into Angular framework internals is beyond my current skill level.
At a loss on how to find my answer in code, I decided to revisit the documentation. Specifically, re-reading Angular Signals developer guide. Since it's a new feature in developer preview, it's still relatively sparse document and not something overwhelmingly long. I hoped to find an answer here and the most promising information is about Injection context. It explained effect()
requires something called an injection context and gave a few examples of how to make sure an effect()
has what it needs. The document didn't say what happens if an effect()
did not have an injection context so I have no idea if my problem match the expected symptoms. Still, it was worth a shot.
For the code lab, we were instructed to put our effect()
inside the ngOnInit()
function. I don't know if ngOnInit()
has an injection context, but it isn't in any of the examples listed in documentation. I followed the first example of putting my effect()
in the constructor. Once that code was moved, my console.log()
messages inside started sending data to the developer console and, when I solved the cipher, I see a pop of confetti. Success!
I'm glad that worked, because it was a stab in the dark and I'm not sure what I would try next if it had not worked. So how did the StackBlitz example's effect()
throw up confetti upon completion if ngOnInit()
had no injection context? Comparing code I see a difference: in the StackBlitz project, the prompt "// TODO(3): Add your first effect()
" was in the constructor and not ngOnInit()
as per both the GitHub repository and code lab instruction text. I had wrongly thought StackBlitz cloned from the same GitHub repository. And now that I know the code is different, it helps explain "How the heck did this even work on StackBlitz" mystery.
I'm glad I got the code lab application up and running correctly locally on my own computer, but there's something weird with its CSS layout I need to investigate.
My code changes are made in my fork of the code lab repository in branch signals-get-started
.