Lightning Component Attribute (aura:attribute) are like member variables on a class in Apex. <aura:attribute>tag is used to add an attribute to the lightning component and Salesforce lightning App.

  • Each attribute must have “name” and “type“.
  • To make attribute as required use required=”true” in the tag.
  • To make default value use default=”string or integer“.

Attribute naming Rules.

An attribute name must follow these naming rules.

  • Must begin with a letter or an underscore.
  • Must contain only alphanumeric or underscore characters.

What is lightning Component Attribute : Aura:attribute

In this Salesforce tutorial, we are going to learn about Lightning Component attributes and how can to use <aura:attribute> tag in Salesforce lightning app and lightning component.

In this Salesforce example, we are going to create an application and a component called “addition”. Here we will add two integers and will find the sum of two integers. And also we add styling to the Salesforce lightning component.

Here we have to define two tags they are <aura:component> and<aura:attribute>. This lightning Component Attribute will have name and type.

lightning Component Attribute

As shown above, we have selected type as integer because we are storing numbers. And if we want to provide default values enter default=”value”. To display two numbers we have to use particular expression like {!v.num1} + {!v.num2} = {!v.sum}.

  • We also defined a button called submit. When a submit button is clicked the result will be displayed on the screen.
  • The sum value will not be displayed in the output when we click on submit because we have not defined what should happen when submit button is clicked.
lightning Component Attribute

Here we have to add a call a method called {!c.add}. Now click on controller in the component and add the controller as shown below.

lightning Component Attribute

When we click on Controller, Controller file will be created automatically. We want to call {!c.add} function.

  • Click on Save button.

lightning Component Attribute output.

When we preview the lightning application, the output will be as follow.

lightning Component Attribute

The look and feel of the output will be changes by adding style to lightning component. Click on Style and add the following css code given below.

Output :

lightning Component Attribute

Conclusion : In this Salesforce tutorial, we have learned about lightning Component Attribute and created Salesforce lightning component for addition of two integer. In our upcoming Salesforce training, we will learn about Salesforce lightning Component composition with an example.