In this tutorial, I am going to show you how to use Attribute in Lightning Component Salesforce. Attributes in Lightning Component can be thought as a member variable of a class. This has some type and used to store data.

Attribute in Lightning Component Salesforce:

Attributes in Lightning Component can be thought as a member variable of a class. This has some type and used to store data.The attribute can be used for creating a dynamic component. <aura:attribute> tag is used for creating an attribute in a lightning component. To use the value of an attribute we use the expression syntax.

The syntax for declaring Attribute in Lightning Component Salesforce:

<aura:attribute name="attrname" type="String" default="test value" description="description goes here" access="public"/>

Where,

name (required): Name of an attribute.

type (required): Type of information to be stored.

default(optional) : Default value of an attribute.

access (optional): Indicates whether the attribute can be used outside of its own namespace. Possible values are public (default), and global, and private.

description (optional): Summary of an attribute and its usage.

required (optional): represent a boolean value. Determines if the attribute is required. The default is false.

Access attribute in Lightning Component.

To access attribute in Lightning Component use the expression:

{!v.attrname}

Example of Attribute in Lightning Component:

Create a lightning component called Attribute Demo.

<aura:component >
 
<aura:attribute name="FirstName" type="String" default="Vivek Singh" />
<aura:attribute name="Age" type="Integer" default="25"/>
<aura:attribute name="isMale" type="Boolean" default="true"/>
 
<p>Name {!v.FirstName}</p>
 
<p>{!v.FirstName} is {!v.Age} Years Old.</p>
 
<p>{!v.FirstName} is male? = {!v.isMale}</p>


</aura:component>

Create an App called TestApp and write the following code:

<aura:application extends="force:slds" >
<c:AttributeDemo/>
</aura:application>

Run the App and see the output:

Attribute

Naming Convention for Attribute in Lightning Component:

An attribute name must follow these naming rules:

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

View More:

Conclusion:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestions are important for us.

Thank You.

Leave a Comment