Wednesday, September 5, 2018

What are Attributes in Lightning Components?

Attribute act as a variable to store different types of values.


<aura:attribute name="Anyname" type ="string" description="some description"  default="some default string"  />

 Name and type are a parameter of an attribute that is always required in attributes, the description is another parameter where we can mention about description of attribute. Description is not a required parameter. Similarly, we have a default parameter to give some default value to attribute. To set the access for an attribute we have access parameter where we can specify public, private, global access for an attribute. By default, the attribute have public access.

To make the attribute required we have required parameter where we can set true or false.

Let start with the basic attributes,

1) Boolean


<aura:attribute name="Anyname" type="Boolean" />

We can use the boolean attribute when we want to store boolean results.

2) Integer


<aura:attribute name="Anyname" type="Integer" />

We can use the integer type attribute for storing integer type values.

for ex: 1,2,3 etc.

3) String


<aura:attribute name="Anyname" type="String" />

We can store some text value inside the string type attribute.

ex: some text 1,some text2 etc.

4) Date


<aura:attribute name="Anyname" type="Date" />

5) DateTime


<aura:attribute name="Anyname" type="DateTime" />

6) Double


<aura:attribute name="Anyname" type="Double" />

To store fractional values.

7) Decimal


<aura:attribute name="AnyName" type="Decimal" />

Decimal is better than Double for maintaining precision for floating-point calculations. It’s preferable for currency fields.

8) Long


<aura:attribute name="AnyName" type="Long" />

Long values can contain numbers with no fractional portion. Use this data type when you need a range of values wider than those provided by Integer.

We can use arrays for each of these basic types. 

As an example,

<aura:attribute name="fruitsList" type="String[]" default="['apple','mango','orange']" />

No comments:

Post a Comment