[SalesForce] How to call parent Lightning component’s method in child component

I want to create a Lightning component where i can define some custom actions which can be used by the component which is creating instance. It will be very similar to the onrowselection, onsort actions on lightning:datatable or onclick of lightning:button.

Anybody knows whether it possible for custom components in lightning, if yes then how?

Best Answer

You can pass a reference to the parent into the child, so in the parent component:

<aura:method name="parentMethod" .../>

<c:child parent="{! this }" ... />

and in the child component you can then invoke the parent method:

<aura:attribute name="parent" type="Aura.Component" required="true"/>

someMethod: function(component, event, helper) {
    var p = component.get("v.parent");
    p.parentMethod();
}

This can sometimes be cleaner than using a component event that others have commented about. But best to learn about both techniques.

Related Topic