Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Signal computed functionality doesn't update the number of item an array contain

I have this service

itemList= signal<Item[]>([]);
reset(){
   this.itemList.set([]);
}
add(item){
 this.itemList.update(list => {
  const ind = list.findIndex(o => o._id == item._id);
  if (ind !== -1) {
    list.splice(ind, 1);
  } else {
    list.push({_id:item._id,name:item.name});
  }
  list.sort((a, b) => a.name > b.name ? 1 : a.name === b.name ? 0 : -1);
  return list;
})
}
itemCount = computed(() => this.itemList().length);

reset function and add item is working as expect but the itemCount is not updating, it is always ZERO.

I tried by accessing the service directly in the html : service.itemCount() and it doesn't work. What I'm doing wrong ?

However, if I use service.itemList().length it works.

UPDATE

Please find the issue with angular 17 here.

like image 397
mbagiella Avatar asked Nov 06 '25 06:11

mbagiella


1 Answers

If you don't change the reference, angular will assume the value hasn't changed, and won't update

Just change your return value on update to return a new value

return [...list];
like image 145
Caio Oliveira Avatar answered Nov 08 '25 22:11

Caio Oliveira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!