aura:method

Use <aura:method> to define a method as part of a component's API. This enables you to directly call a method in a component’s client-side controller instead of firing and handling a component event. Using <aura:method> simplifies the code needed for a parent component to call a method on a child component that it contains.

The <aura:method> tag has these system attributes.

Attribute Type Description
name String The method name. Use the method name to call the method in JavaScript code. For example:
cmp.sampleMethod(param1);
action Expression The client-side controller action to execute. For example:
action="{!c.sampleAction}"

sampleAction is an action in the client-side controller. If you don’t specify an action value, the controller action defaults to the value of the method name.

access String The access control for the method. Valid values are:
  • public—Any component in the same namespace can call the method. This is the default access level.
  • global—Any component in any namespace can call the method.
description String The method description.

Declaring Parameters

An <aura:method> can optionally include parameters. Use an <aura:attribute> tag within an <aura:method> to declare a parameter for the method. For example:

<aura:method name="sampleMethod" action="{!c.doAction}" 
  description="Sample method with parameters"> 
    <aura:attribute name="param1" type="String" default="parameter 1"/> 
    <aura:attribute name="param2" type="Object" /> 
</aura:method>
Note

Note

You don’t need an access system attribute in the <aura:attribute> tag for a parameter.

Creating a Handler Action

This handler action shows how to access the arguments passed to the method.

({
    doAction : function(cmp, event) {
        var params = event.getParam('arguments');
        if (params) {
            var param1 = params.param1;
            // add your code here
        }
    }
})

Retrieve the arguments using event.getParam('arguments'). It returns an object if there are arguments or an empty array if there are no arguments.