How to call child component controller function/helper from Parent Component Controller

  • I have requirement to call a child controller function from parent controller whenever the event occurs in parent side.

    for e.g

    ABC is parent component and DCE is child component, this child component has Span of Text tag and X symbol. when I click on x it is clearing the value using child controller. ABC parent component has Form element and child component is one one of in form.

    My requirement is to clear the this child component value when parent component button is clicked.

    here is the below sample code of what am trying because i cannot post actual code here

    ParentComponent.cmp

     <aura:registerEvent name="clrValues" type="c:ClearValues"/>
        <div>
             <c:ChildComponent textValue="test"/>
             <ui:button label="Save"/>
             <ui:button label="Clear" press="{!c.clear}"/>      
        </div>
     </aura:component>
    

    ParentController.js

    ({
        clear : function(component, event, helper) {
            var evtFire = component.getEvent("clrValues");
    
            evtFire.fire();
        }
    })
    

    ClearValues.evt

    <aura:event type="APPLICATION" description="Event template" />
    

    ChildComponent.cmp

    <aura:component >
        <aura:handler name="setValues" event="c:ClearValues" action="{!c.clear}"/>
        <aura:attribute name="textValue" type="String" default="test"/>
        <div >
        <ui:inputText label="Expense Name" value="{!v.textValue}" aura:id="input1"/>
        </div>
    
    </aura:component>
    

    ChildController.js

    ({
        clear : function(component, event, helper) {
            helper.clearValues(component,event);
        }
    })
    

    Unless you provide the code ,its hard to help you

    Mohit , added the code i cannot post my original code, but i have given sample what i was trying to achieve.

  • prashanthkr

    prashanthkr Correct answer

    5 years ago

    The handler name in your child component should match the registered name in the parent component. i.e., the handler in child component should be like this:

      <aura:handler name="clrValues" event="c:ClearValues" action="{!c.clear}"/>
    

    --UPDATE--

    I think a better approach for parent-to-child communication is to use <aura:method> as listed in lightning docs and here

    To do this, you should define a method in child component like this:

      <aura:method name="sampleMethod" action="{!c.clear}" access="PUBLIC"> 
    

    You should then have an id when you refer it inside your parent component like this:

      <c:ChildComponent textValue="test" aura:id="cComp"/>
    

    Then in the clear method of the parent component, you should invoke the child method like this:

     var childCmp = component.find("cComp")
     childCmp.sampleMethod();
    

    It applies only for component level event,not for an Application Event.

    hmm that's true. I didn't notice this is an application event. @Raj_SFDC, why can't you make your event a component event?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM