Angular: How to check if output() has an observer

Angular: How to check if output() has an observer

I stopped using the @Output() decorators since the new output() function got released.

I got my dummy component:

export class TaskComponent implements OnInit {
    readonly taskStarted = output<number>();

    taskStartedClick(id: number) {
        this.taskStarted.emit(id);
    }
}

I am using it in my parent component:

<app-task (taskStarted)="doStuff($event)"></app-task>

I am trying to figure out whether the emitted event is observed by the parent component or not.

Using the old @Output decorator, you could go ahead and check whether it is observed or not, e.g:

ngOnInit() {
    const isObserved = this.taskStarted.observed;
}

But I can't do the same with output(), since it is an OutputEmitterRef and not an EventEmitter.

Does anybody else know perhaps another solution or an alternative? I have tried and searched in the internet, but it is very difficult to find solutions for the new output() function, instead you get mainly solutions for the @Output() decorator.

Answer

The recommendation today is to have an Observable (which has the observed property and to use outputFromObservable.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles