Lightning Component – Attributes

Attributes are used to perform the input and output operation in the Lightning Component or App. Attributes are just like Variables in the Apex Class. Need to use <aura:attribute> tag to declare the attribute in the Lightning Component or App.

Definition of <aura:attribute> tag

How can we access the attribute value?
Using this expression {!v.attributeName} In this case, it would be {!v.message}.

v means (View) Value Provider (represents in View Layer) which assists to access related data.

Attribute Types

  1. Primitives Data Types: String, Integer, Decimal, Double, Long, Date, Date/Time,  Boolean
  2. Collection: Array, Set, List, Map
  3. sObjects: StandardObject, CustomObject__c
  4. Custom Apex Class
  5. Framework Types: Aura.component, Aura.Component[]

Let’s GO

Step 1: Using the Same Lightning Component “Welcome.cmp” and same CSS file “Welcome.css” (Please create, if it’s not there)

Welcome.cmp | Attribute Declaration

<aura:component >    
    <!-- attributes declaration -->
    <aura:attribute name="message" type="String" default="Hello" description="Used for showing the message in Lightning Component"/>  
     <div class="slds slds-m-top--x-large slds-align_absolute-center tagLine">
    	<!-- access the attribute -->

Welcome.css | Describes Styling

.THIS.tagLine {   

Step 2: Calling from Lightning App “” with message attribute

<aura:application extends="force:slds">
    <c:Welcome message="Welcome to Everyone !!"/>

Step 3: Result

Welcome Message - attributes

Step 4: Calling from Lightning App “LearningLightning.App” without message attribute

<aura:application extends="force:slds">

Step 5: Result

It will show “Hello” as the default value of the “message” attribute(in Step 1) due to we have not passed the value of “message” attribute from the App to Component.

hello - attributes

3,106 total views, 1 views today

Rating: 5.0/5. From 4 votes.
Please wait...
This entry was posted in Lightning Components. Bookmark the permalink.

1 Response to Lightning Component – Attributes

  1. Iam says:

    Sir, Nice explanation. This is really help. Keep posting. Cheers

    No votes yet.
    Please wait...

Leave a Reply