I have thought experiment that derived from case where I needed to sometimes add and remove function for Output of my Angular component. This is the example
Parent component will contain only output function for child component <my-component>
.
ParentComponent
class ParentComponent{
myOutputFunction = null;
setOutputFunction() {
this.myOutputFunction = () => {
// do something
};
}
removeOutputFunction() {
this.myOutputFunction = null;
}
}
Template for ParentComponent
<my-component (someOutput)="myOutputFunction($event)"><my-component>
Child component MyComponent
will have output someOutput
inside. This will serve also as a question how to detect when parent decided to do removeOutputFunction()
and potentially subscribe to that event and react to it.
MyComponent child component
class MyComponent {
@Output() someOutput = new EventEmitter();
ngOnInit(): void {
// At this point we can see if there are any observers to someOutput
if(this.someOutput.observers.length > 0) {
console.log('YEY we have a set output method from ParentComponent to MyComponent`s attribute');
} else {
console.log('We have no attribute defined');
}
}
}
In ngOnInit()
we can check if we have coded (someOutput)="myOutputFunction($event)"
.
So, the conclusion.
Is there a way to listen (subscribe) to change of someOutput
from parent from () => // do something
value to null
value.
When i log this.someOutput.observers
i get array of Subscribers.
Can I use this.someOutput
as EventEmitter in some way to get to where this experiment is pointing to?
Thanks