Signals and their inputs are great. I’m curious if there is a good way of using input changes beyond ngChanges()
, rxjs-observables
or even the new effect()
.
Is there a way to react to input-changes without those three?
Let’s assume we have a component “member” which has a input “id”. Whenever “id” changes, I want to reload data from a “MemberService”.
With ngChanges()
I would do it like this:
@Input
id: number | undefined
ngOnChanges(changes: SimpleChanges) {
if(changes.id) {
this.memberSrv.load(this.id()).then((data: any) => this.data = data));
}
}
With new signals
like this:
id = input.required<number>();
constructor() {
effect(async ()=>
this.data = await this.memberSrv.load(this.id())
);
}
The flaw on the signal-approach is that this.id() isn’t visible on first sight. It’s somewhere in the effect method.
We can also wrap signals within rxjs-Observables with “asObservable”, but then we rely on rxjs plus the signal is still wrapped/”hidden” in a function: asObservable(this.id)
Is there any way to subscribe to a input without using effect() or rxjs?
What is best practise to react on inputs?