Sub topic 15
Injecting a parent component through an abstract token
This chapter assumes you know standalone components, services, and the element injector tree from the earlier chapters. It also assumes you know the optional option of inject().
The problem
A child component sometimes needs to call its parent. A close button inside a dialog is a typical case. The obvious fix is to inject the parent class directly. That makes the child import the concrete parent, and the parent often imports the child, which creates a circular import. It also ties the child to one parent implementation.
Angular's element injector tree offers a cleaner route. The parent registers itself under an abstract token, and descendants inject that token.
The pattern
import { Component, forwardRef, inject } from '@angular/core';
export abstract class Panel {
abstract close(): void;
}
@Component({
selector: 'app-dialog',
standalone: true,
providers: [{ provide: Panel, useExisting: forwardRef(() => DialogComponent) }],
template: `<ng-content />`,
})
export class DialogComponent extends Panel {
close(): void {
// hide the dialog
}
}
@Component({
selector: 'app-dialog-close',
standalone: true,
template: `<button (click)="panel?.close()">Close</button>`,
})
export class DialogCloseComponent {
protected readonly panel = inject(Panel, { optional: true });
}A parent template then projects the close button into the dialog:
<app-dialog>
<app-dialog-close />
</app-dialog>How it works
Each piece does one job:
- The abstract class is the token. TypeScript interfaces are erased at compile time, so they cannot be injection tokens. An abstract class survives as a runtime value and also gives the child a typed contract.
useExistingcreates an alias. It tells the injector to return the component instance already created for this element. Without it,useClasswould build a second, separate object.forwardRefdelays the reference. The decorator runs before theDialogComponentclass binding is initialized, so a direct reference would be undefined at that point.optional: truemakes the parent optional. If the child is used outside a dialog,panelisnullinstead of aNullInjectorError.
Use providers, not viewProviders
Here <app-dialog-close /> is written in the parent's template and projected through <ng-content />. Its position in the element tree is inside app-dialog. Providers declared in providers are visible to projected content. Providers declared in viewProviders are visible only to the component's own template, so a projected child would not find Panel. If you move the registration to viewProviders and the button stops finding its parent, this is the cause.
Common mistakes
- Using
useClassinstead ofuseExisting, which creates a second instance that is not the one on screen. - Defining
Panelas an interface, which fails because the token does not exist at runtime. - Forgetting
optional: truewhen the child may appear outside any parent.
Exercise
A close button is projected into <app-dialog> with <ng-content />. The dialog registers Panel in viewProviders, and the button injects Panel with optional: true. What value does the button get, and how do you fix it?
Show answer
The button gets null. viewProviders are hidden from projected content, so the lookup does not find them. Move the registration to providers so the projected button can resolve Panel.