Reading and writing signals
A writable signal is indicated by the signal type from the @angular/core npm package.
You will need the source code of the Angular application we created in Chapter 6, Reactive Patterns in Angular, to follow along with the rest of the chapter. After you get the code, we suggest you remove the key-logger folder for simplicity.
Let’s get started and learn how we can write a value in a signal:
- Open the
app.component.tsfile and import thesignalartifact from the@angular/corenpm package:import { Component, inject, signal } from '@angular/core'; - Declare the following property in the
AppComponentclass as asignaland initialize it:currentDate = signal(new Date()); - Replace the
timestampvariable in thesetTitleproperty with the following snippet:this.currentDate.set(new Date());
In the preceding snippet, we use the set method to write a...