We have Angular 19 and still is not possible to subscribe to output events, but there are two strategies to handle output events when using ngComponentOutlet:
1. Inject a function using ngComponentOutletInjector and handle the behavior outside the component:
@Component({
template: `
<ng-container [ngComponentOutlet]="addressForm"
[ngComponentOutletInjector]="injector">
</ng-container>`
})
export class ParentComponent {
private injector = Injector.create({
providers: [
{
provide: 'onSubmit',
useValue: (data: any) => this.handleSubmit(data)
}
]
});
}
2) Get a reference to the component using ViewChild. This a powerful Angular tecniques as it gives you access to all component properties, methods, and events:
@Component({
template: `
<ng-container #componentRef
[ngComponentOutlet]="addressForm"
[ngComponentOutletInputs]="{ isBilling: isBilling }">
</ng-container>`
})
export class ParentComponent implements OnDestroy {
@ViewChild('componentRef', { read: YourComponentType })
component!: YourComponentType;
private subscription: Subscription;
ngAfterViewInit() {
// Check if the output exists before subscribing
if (this.component.someOutput) {
this.subscription = this.component.someOutput.subscribe(
data => this.handleOutput(data)
);
}
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
Assuming YourComponentType is an abstract class or parent component, all your component should have same outputs methods, otherwise validate in your code the subscription exist before actually subscribe and don't forget to unsubscribe when your component is destroyed.