Newer Version Available

This content describes an older version of this product. View Latest

Working with Components

Apex uses a simple dot notation to work with components and component attributes.

To reference a component, use Cmp.<myNamespace>.<myComponent>; for example, Cmp.ui.button.

To reference a component’s attribute, use Cmp.<myNamespace>.<myComponent>.<myAttribute>; for example, Cmp.ui.button.label.

Creating a Component in Apex

To create a component, use this syntax:

1Cmp.<myNamespace>.<myComponent> cmpVar = new Cmp.<myNamespace>.<myComponent>();

For example:

1Cmp.ui.button button = new Cmp.ui.button();

You can also include attributes when you’re creating a component. For example:

1Cmp.ui.button button = new Cmp.ui.button(label = 'Click Me');

Updating a Component Attribute

You can update an attribute value in a component by assigning a new value. For example:

1Cmp.ui.button button = new Cmp.ui.button(label = 'Click Me');
2String buttonLabel = button.label;
3button.label = 'Click Me Not';

Accessing the Current Component

Use Aura.getComponent() to access the current component in the component’s controller. For example, in a button’s controller, you could access the button component like this.

1Cmp.ui.button button = Aura.getComponent();