How to call one controller function from another controller function in Lightning?

If I have two functions foo() and bar() in my Lightning component controller, how can I call one function directly from another function, without having to queue it up through $A?

Here’s a sample controller setup:

({
    bar : function(component, event, helper) {
        // Do something cool
    },
    foo : function(component, event, helper) {
        // How do I call bar() from here?
    }
})

Answer

I think that the better approach would be to try to extract whatever it is that you need to call in that controller function into the helper and call the helper. The helpers exist to enable sharing code within a component. Do you have a use case where that wouldn’t work?

Just for fun, inspecting the action object the following would work:

var action = component.get("c.bar");
action.$meth$();

Obviously don’t do that! 🙂

Attribution
Source : Link , Question Author : Marty C. , Answer Author : Peter Knolle

Leave a Comment