Yesterday, I wrote about some best practices around exposing a Signal in our Angular applications. Let’s now take a look at the two different ways a Signal can be updated.
set()
The easiest way to update a Signal is the set()
method. Nice and easy for basic data types such as strings or booleans:
update()
When the new value of a Signal depends on its previous value, update()
is the best method to use. This is the ideal method for a counter, for instance:
Here is how to increment the counter based on its current value:
TL;DR
- When you need to update a simple value (string, number, boolean), use
set()
. - If that new value is based on the previous one, use
update()
instead ofset()