r/angular • u/HamsterFinancial2616 • 1d 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?
14
u/Thrawn_86 1d ago
Why not using an input-signal and effect
1
u/Maleficent-Back-6527 23h ago
Why do you even need to do that in the component. Since your child component is simply setting the new value from the parent into the service, it seems to me like you don’t even need a child component for that. You could directly update your service signal from the parent component!
1
u/Johalternate 21h ago
If
userIdis an input to a component, it means that the parent component getsuserIdfirst, how does the parent component getuserId?You can do this in many different ways depending on where
userIdoriginates, if you could share that detail we could provide a better approach.3
u/sebastianstehle 1d 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.
2
u/MrFartyBottom 1d 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.
2
u/SensitiveHearingLoss 19h ago
Assuming that your parent component passes a value into the child component like this:
@for (post of posts(); track post.id) {
<app-post-comments [postId]="post.id" />
}
Your component receives the input as such:
export class PostCommentsComponent {
private readonly commentsService = inject(PostCommentsService);
readonly postId = input.required<number>();
readonly comments = this.commentsService.getComments(this.postId);
}
Then your service can simply contain a method which expects a signal:
@Injectable({ providedIn: 'root' })
export class PostCommentsService {
getComments(postId: Signal<number>) {
return httpResource<Comment[]>(
() => `/api/posts/${postId()}/comments`
);
}
}
You don’t need effects or anything. When the input value changes, the service method will be triggered.
(I hope)
1
u/MarioShroomsTasteBad 12h ago
Yeah I feel like an effect off of a signal input works here. If the id is in the route you could also use the new router resource api, or traditional data resolver.
1
u/Dull-Structure-8634 23h ago
I think you're trying to shoehorn signals where they don't necessarily need to be there. You already have what you need: RxJs.
Use transform your input signal into an observable with toObservable, subscribe to it in the constructor and voilà, it's working, simple and easy to understand.