r/Angular2 • u/FewDot9181 • Apr 10 '26
Fuctions passed as props in Angular
I have seen many ways but how do I do it in modern Angular, where I send a function from a parent component to a child component and I want that same function called from the child component.
5
Upvotes
13
u/sh0resh0re Apr 10 '26
Sounds like you want to dispatch an output event from the child to the parent to call that parent function.
3
u/ldn-ldn Apr 10 '26
Use outputs.
2
u/beartato327 Apr 11 '26
Wouldn't a signal be the most modern way over an output?
2
u/toasterboi0100 Apr 10 '26
Unless you want this for various getter functions (i.e. a select component using a getter for each options label) then you really should not do this.
1
u/Icy_Reputation_2209 Apr 11 '26
You should first ask yourself two questions:
If the answers are no, you are probably trying to implement a „strategy pattern“. You can then just pass the function to the component like any other input, but make sure that the function is properly bound if it uses this anywhere.
1
u/Jitos Apr 12 '26
Don’t, just use a signal
1
u/Dry-Echo-5406 Apr 10 '26
That's bad practice IMO. Why could you not just use "output"?
0
u/Corum0407 Apr 10 '26
Looking at the answers here, I think an example could help ppl understand what you want to achieve.
The easiest way would be to use a service and call the function in the parent and the child.
Another way maybe with inputs something like?:
0
u/thedrewprint Apr 12 '26
I think you want a library called React.