Search code examples
angulartypescriptrxjsangular-reactive-formsangular-forms

Does Angular automatically unsubscribe FormControl.value subscriptions?


I'm looking at this code snippet that uses untilDestroyed:

      this.control.valueChanges.pipe(untilDestroyed(this)).subscribe(c => {
        this.update.emit(c);
      });
    }

Does Angular automatically unsubscribe the FormControl.valueChanges observable on component destruction, or do we need the operator?

Part of the reason I ask is that my understanding is that Angular unsubscribes Observables used in template expressions automatically, so I thought perhaps it might have a "Magic" way of doing it for FormControl instances as well?


Solution

  • You should unsubscribe in ngOnDestroy component callback.

    Since stream is never closed, subscription will stay.