0

Before signals, I had an observable that I would watch to trigger a FormControl's editable property, like this:

this.#isItEditor$
    .pipe(takeUntilDestroyed(this.#destroyRef))
    .subscribe(x => {
        const funded = this.formGroup.controls.funded
        if (x)
            funded.enable()
        else
            funded.disable()
    })

Now I've changed from an observable to a signal, but it feels like, in this case, I still need to create an observable from the signal to then do the pipe/subscribe the same way I used to.

I'm not assigning anything based on the signal changing, I'm just implementing a side effect. Is this correct?

Gargoyle
  • 9,590
  • 16
  • 80
  • 145

1 Answers1

0

You could use effect to listen signal changes. Effect will track signal reads and whenever value changes effect runs again.

effect(() => {
  this.#isItEditor(); //Read signal here
  //Rest of the logic
    const funded = this.formGroup.controls.funded
    if (x)
        funded.enable()
    else
        funded.disable()
  
});
Rap
  • 6,851
  • 3
  • 50
  • 88
Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
  • It seems like, using an `effect`, any time any signal runs it would have to process all of them. Is that true? If so, I'd probably be better letting it convert to an `Observable`. – Gargoyle May 12 '23 at 23:39
  • 1
    effect only runs whenever their dependencies change – Chellappan வ May 13 '23 at 01:52