Context:
With Angular 16, we can now use Signals.
But it doesn't seem clear to me how to do a simple push into an array.
The documentation suggests from Angular Signal official doc:
When working with signals that contain objects, it's sometimes useful to mutate that object directly. For example, if the object is an array, you may want to push a new value without replacing the array entirely. To make an internal change like this, use the .mutate method
So it would be for example:
messages = signal<Message[]>([]);
ngOnInit(): void {
const newMessage = ...
this.messages.mutate(values => values.push(newMessage));
}
But it seems pretty weird to refer 2 times to the list. The first time with this.messages
and the second one with values
.
Question:
How can I push an item into a Signal array?