Friday, February 11, 2022

How to Call A Method From Another Method in Same Lightning Controller

 Hello Friends today we are going to discus about how to call a method from  another method in Aura component . Please find he sample code for the same.

Component 

<aura:component>

    <!--Component Start-->
    <div class="slds-m-around_xx-large">
        <lightning:button variant="Brand" class="slds-button" label="Submit" onclick="{!c.methodFirst}"/>
    </div>
    <!--Component End-->
</aura:component>

Component jS
({
    //Method First
    methodFirst : function(component, event, helper){
        //Call methodSecond from methodone
        var action = component.get('c.methodSecond');
        $A.enqueueAction(action);
    },
     
   //Method Two
    methodSecond : function(component, event, helper){
        alert('Method Second');
    }
})

As per above code we can call do the code.

Methods in the same controller cannot talk to each other because this will always be undefined in the controller. You need to use the helper for this case..

Although there's a way this can be done using aura:method, see here. I would strongly recommend you to use helper instead of aura:method.

No comments: