Newer Version Available
Using the Aura.Action Attribute Type
Example
This example demonstrates how to pass an action handler from a parent component to a child component.
Here’s the child component with the Aura.Action attribute. The onclick handler for the button uses the value of the onclick attribute, which has type of Aura.Action.
1<!-- child.cmp -->
2<aura:component>
3 <aura:attribute name="onclick" type="Aura.Action"/>
4
5 <p>Child component with Aura.Action attribute</p>
6 <lightning:button label="Execute the passed action" onclick="{!v.onclick}"/>
7</aura:component>Here’s the parent component that contains the child component in its markup.
1<!-- parent.cmp -->
2<aura:component>
3 <p>Parent component passes handler action to c:child</p>
4 <c:child onclick="{!c.parentAction}"/>
5</aura:component>When you click the button in c:child, the parentAction action in the controller of c:parent is executed.
Instead of an Aura.Action attribute, you could use <aura:registerEvent> to register an onclick event in the child component. You’d have to define the event and create an action in the child’s controller to fire the event. This event-based approach requires a few extra steps but it’s more in line with standard practices for communicating between components.