Loading...
save

Angular computed signal doesn’t update when I push to an array

clock icon

asked 4 hours ago

message icon

0

eye icon

6

I’m trying signals in Angular for the first time. I have a list of todos in a signal and a computed signal that counts the open ones. When I add a todo, the list in the template updates, but the counter stays at the old number.

ts

1todos = signal<Todo[]>([]);
2
3openCount = computed(() => this.todos().filter(t => !t.done).length);
4
5addTodo(title: string) {
6 this.todos().push({ title, done: false });
7}
1todos = signal<Todo[]>([]);
2
3openCount = computed(() => this.todos().filter(t => !t.done).length);
4
5addTodo(title: string) {
6 this.todos().push({ title, done: false });
7}

html

1<p>{{ openCount() }} open tasks</p>
1<p>{{ openCount() }} open tasks</p>

I thought computed() tracks the signal automatically. Do I have to call something to trigger it, or am I using signals the wrong way?

Angular version is 18.

0 Answers

Empty state illustration

No Answers Found

The answer board is empty. Make it rain with your brilliant answer.

1

Write your answer here

Top Questions