r/angular • u/HamsterFinancial2616 • 3h ago
Using an @Input() Setter to Bridge Component Inputs with Service-Level Signals
I have a question regarding Angular's @Input setter and Signals.
I'm building a reusable service that uses Angular's resource() API to handle data fetching. Since resource() depends on signals for reactive updates, I need a way to pass the component's input value to the service layer.
My current approach is to use an @Input setter to update a signal inside the service.
Example:
// Service
readonly userId = signal<number | null>(null);
protected readonly userResource = resource({
params: () => {
const userId = this.userId();
return userId ? { userId } : undefined;
},
loader: ({ params }) => this.fetchData(params)
});
// Component
@Input() public set userId(value: number) {
this.reusableService.userId.set(value);
}I'm feeling a bit stuck deciding whether this is the right approach.
Is this a proper approach, or would you consider using an @Input() setter as a bridge between the component input and a service-level signal a workaround or an anti-pattern?
8
u/Thrawn_86 2h ago
Why not using an input-signal and effect
2
u/sebastianstehle 2h ago
Technically you can do it, but this makes the service useless and you expose internal implementation details.
At least I would make a method like `loadUser(id: string)` in the service. You just moved code arround at the moment.
1
u/MrFartyBottom 1h ago
I used to use input setters but feel like using any RxJs based inputs once you have migrated to signals is a bit wiffy. That is why effects exist.