Sub topic 3
Injection Context, Cleanup, and Signal Interop
Assume you know basic TypeScript, Angular components, dependency injection, and the purpose of change detection. This chapter focuses on lifecycle rules and boundaries between signals and other state tools.
Injection context, cleanup, and destroying effects
An injection context is an Angular-managed place where dependency injection and lifecycle registration are available. A component constructor is an injection context, so an effect created there is automatically destroyed with the component.
Use onCleanup for timers, subscriptions, or other work that must stop before the effect runs again or is destroyed.
import { Component, effect, input } from '@angular/core';
@Component({
selector: 'app-delayed-log',
template: '',
})
export class DelayedLogComponent {
id = input.required<number>();
constructor() {
effect((onCleanup) => {
const currentId = this.id();
const timer = setTimeout(() => {
console.log('loaded', currentId);
}, 500);
onCleanup(() => clearTimeout(timer));
});
}
}When id changes, Angular runs the cleanup before scheduling the next effect run. When the component is destroyed, Angular performs the final cleanup. Creating an effect outside an injection context requires an explicit injector and should be reserved for code that controls its lifetime.
Reading signals without tracking them
Every signal read during an effect normally becomes a dependency. Use untracked when a read supplies information for the current operation but should not trigger another run.
import { effect, signal, untracked } from '@angular/core';
const query = signal('');
const page = signal(1);
effect(() => {
const text = query();
untracked(() => console.log('search', text, 'page', page()));
});Changing query reruns the effect. Changing page does not.
Custom equality functions
Computed signals normally use equality checks to decide whether a new value differs. A custom equal function is useful when a calculation creates a new object or array with unchanged contents.
import { computed, signal } from '@angular/core';
const items = signal([{ id: 1, done: true }]);
const doneItems = computed(() => items().filter(item => item.done), {
equal: (a, b) => a.length === b.length &&
a.every((item, index) => item.id === b[index].id),
});`linkedSignal` for state that resets
Use computed() for read-only derived state. Use linkedSignal() when state has a current value but should reset or be recalculated when another signal changes, such as a selected option when the available options change.
Bridging signals and RxJS
toSignal() converts an observable into a signal and manages its subscription through the injection context. toObservable() exposes signal changes as an observable. Use these adapters at boundaries instead of duplicating state.
Exercise
Which tool should hold a value derived from other signals: computed(), effect(), or an event handler?
Show answer
Use computed(). Use effect() for external side effects and an event handler for direct user actions.