How to modify a Signal in Angular upon an event to change a nested property?

I have a setup in my Angular project where I’m using signals to manage state. Specifically, I have a projectSignal that holds a Project object which can contain an array of Task objects, and each Task can have a Permission object.

Here’s the current setup:

projectSignal: Signal<Project | undefined> = toSignal(this.store.select(selectActiveProject));
permission: Signal<Permission | undefined> = computed(() => this.projectSignal()?.tasks?.[0]?.permission);

And the corresponding HTML:

  <app-permission [permissionsSelected]="permission"></app-permission>
}

This works well for conditionally rendering the component based on the existence of the permission object.

I also have an Output and I’m listening to this output in the constructor:


constructor(@Inject(MAT_DIALOG_DATA) public data: DialogData, private store: Store<AppState>) {
  effect(() => {
    this.updatePermission().output.subscribe((test: any) => {
      // Logic to handle the event
    });
  });
}

Now, I would like to modify the projectSignal and consequently the permission signal when a certain event occurs (for example, a button click). How can I update the projectSignal in response to an event (in effect()) , so that the permission signal reflects the new state?

Additional Information:

This question follows up on a previous one : Best Practices for Nested Optional Checks in Angular Template with Signals

The goal is to change the permission value when a user interacts with the UI, such as clicking a button.

What i tried

  1. Change Signal to WritableSignal
  2. change the computed signal to a new one build in the effect
  3. I’m unable tu use update() or set() from angular signals…

Any guidance or best practices on how to achieve this would be greatly appreciated! Thank you!

This code looks like a memory leak, since for each effect trigger, the subscriptions are not destroyed and will keep on piling, causing the application to crash with sufficient items. You need to remove the effect and just subscribe to changes. Or unsubscribe the previous subscription then create a new one. Let me know if you need that code snippet

constructor(@Inject(MAT_DIALOG_DATA) public data: DialogData, private store: Store<AppState>) {
  // effect(() => {
    this.updatePermission().output.subscribe((test: any) => {
      // Logic to handle the event
    });
  // });
}

When you click any button, trigger an event, which updates the root store selectActiveProject this will trigger the signal (projectSignal) to change, which will trigger the compute for permission. Since the code is not shared unable to analyze this question more than this.

Another way is to just use a signal store, instead of a conventional store.

2

I found a solution,
I initialized an attribute Permission in the component and implemented it on the effect

projectSignal: Signal<Project | undefined> = toSignal(this.store.select(selectActiveProject));
permission!: Permission;
childRef: Signal<ChildRef> = viewChild.required(ChildRef)

And in the effect in the constructor

  constructor(@Inject(MAT_DIALOG_DATA) public data: DialogData) {
    effect(() => {
      this.permission= this.projectSignal()?.tasks?.[0]?.permission
      this.childRef().output.subscribe((output: any) => {
        if (output && this.permission) {
          this.permission= {
            ...this.permission,
            statut : true
          };
        }
      })
    })

It works well, i can update the attribute when i want

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật