signal
function is used in Angular to create an initialize signals.
Here we initialize a signal with the value 123
.{{counter()}}
binds to the counter signal.
This ensures that whenever the counter
value changes, the displayed value in the template is updated automatically.{{ counter() }}
with the value of the property, so the output will be 123
message
, initialize it with the string hello
and display it in the templateset
method directly assigns a new value to the signal.
It's straightforward and typically used when you need to replace the current value with a completely new one.update
ensures that the state change is atomic and avoids potential race conditions.
This is particularly useful in scenarios where multiple updates might occur in quick succession or concurrently.counter
by 2counter
value to be greater than 10this.counter()
in your classcounter = signal([])
counter = Signal([])
counter = new Signal([])
effect
in Angular provides a declarative way to handle side-effects based on reactive state changes.effect
that reacts to changes in the counter
signal.effect
function is invoked every time the counter
value change, we can use this approach to trigger a side effect every time the counter
changes.counter
value or, just like in this example, we can print a log
and we save the value into localStorage
.effect
functions.
So, in this exercise you should:message
, with and empty string as initial valueeffect
function that display a console.log
when message
changesconsole.log
is displayed each time a button is clicked.
Furthermore, the value is also saved on localStorage that you can inspect by using your browser Dev Tools -> Application Tab.effect
functions inside the same constructor.effect
s are invoked immediately upon setup and whenever their respective reactive dependencies change, in this example counter
and message
.inc()
method, only the first effect
will be invoked.randomMsg()
is invoked.effect
function that display both values and it's then triggered when each of the signals change