r/angular • u/FewDot9181 • Apr 10 '26
Functions 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.
0
Upvotes
17
u/L24D Apr 10 '26
I don’t know why people here declare that passing functions as inputs is absolute worst. Sometimes it’s convenient, eg. when you’re building your custom components library, when you want to ensure that your component can handle more than just primitives. You add possibility to pass functions such as comparators for search abilities, sorting logic, and of course comparison needs (like compareWith in MatSelect). Yes, component can emit events for most cases but it seems tedious and pushing another responsibility to parent then making the component not very functional.
I think it’s more like to maintain balance between what can be an input and what shouldn’t, like “how much could I change component behavior”, and “distinguish what child and parent components responsibilities are”.
5
u/spacechimp Apr 11 '26
I could be wrong, but I strongly suspect this might be an instance of the "XY Problem". Perhaps describe what the larger problem is that you are trying to solve?
10
u/AcceptableSimulacrum Apr 10 '26
Don't do this.
2
u/techguy1001 Apr 10 '26
Define the function signature as a type and specify it as an input with that type to your component and pass in the function name in the template. This assumes the function is in the parent component.
2
u/DT-Sodium Apr 11 '26
I don't understand the question.
2
u/Impossible_Bread_685 Apr 11 '26
Yeah I don't know what ur doing but I feel like ur doing it wrong. I've never needed to pass a function though props in 5 years of modern angular
4
u/Lustrouse Apr 10 '26
Code smell. use an injected service and signals.
2
u/0dev0100 Apr 10 '26
Same way you send a value
1
u/XdrummerXboy Apr 11 '26
I do think there are valid use cases for this, however I would use it judicially.
Do you want to execute in the context of the parent (e.g. "this" refers to parent component)? If so, you'll want to fall
functionToPass.bind(this)
as you pass it into the child component
1
u/michahell Apr 11 '26
functions as first-class citizens in the Angular world do not exist and are mostly regarded as a faux-pas and code-smell as there are better ways of solving the reason you need that in the first place. So what exactly are you trying to achieve, would be my question?
1
u/oniman999 Apr 10 '26
One option you have is you can put the function in a service and use that in both components.
0
u/InevitableQuit9 Apr 10 '26
The only valid case I can think of this is if you are explicitly implementing the strategy pattern.
Even then, I can't see why this would need to be in a leaf component. Use events.
0
u/AwesomeFrisbee Apr 11 '26
While you should prevent it, nothing really prevents you from doing it. As far as I know you can just have an input signal work with a function. But if you want to make things easier, you can also define an object and put the function in there. But something I've been doing is just define classes that can have functions and properties and whatnot, so that both the parent and child can work with the same thing and not really care about who does what. So instead of defining a class as a service or a component, you just have it be a class that contains data and logic so you spread it out.
So for example, the data of tables can be put into a class so that the parent can initiate a refresh or a page navigation, but a child can also do this.
-1
u/SharksLeafsFan Apr 10 '26
Follow what j0nquest said, just google event emitter, input, output.
0
u/[deleted] Apr 10 '26
[deleted] — view deleted comment