An aura attribute is like a variable that we use in programming language or in Apex class. These are typed fields and set on a specific instance of a component. These attributes can be used inside the component as expressions and in controller and helper as well.
Types of Attributes:
A: Basic Types:
These are the basic attributes that contain values of primitive types like integer, string, boolean etc.
1. String :- It stores simple text values like color, name etc.
2. Boolean :- It stores boolean values like true or false.
3. Decimal :- It stores values with fractional part. Used to store values like price.
4. Integer :- It stores integer/Number values like quantity etc.
5. Double :- It stores fractional values like price, currency values.
6. Date :- It stores date values in yyyy-mm-dd format.
7. DateTime :- It stores date values with timestamp.
8. Long :- It stores non fractional values with wider range than integer.
B: Collection Types:
Lightning supports the collection type attributes like array, list and map.
- Array : An array of items of a defined type
- List: An ordered collection of items.
- Map: A collection that maps keys to values. A map can’t contain duplicate keys. Each key can map to at most one value. Defaults to an empty object, {}.
- Set: A collection that contains no duplicate elements. The order for set items is not guaranteed. For example, “[‘red’, ‘green’, ‘blue’]” might be returned as blue,green,red.
Attributes of these types are typically used in iterations in lightning to display data.
C: Object Types
An attribute can have the type as “Object”. The object is stored in json format.
How to declare different types of attributes in component. See below image:-
Note : The type value of an attribute is case insensitive, and also when you use a particular attribute in an expression the name of the attribute should match with the name specified in attribute definition i.e. the name inside expressions is case sensitive.
Parameters of attribute:
Access : Indicates whether the attribute can be used outside of its own namespace. Possible values are public (default), and global, and private.
Name : The name of the attribute. It is required and must be provided.
Type : Type of the attribute. It could be any of the type that we discussed above like String, Boolean etc.
Default : The default value that you want to provide to the attribute.
Required : It contains boolean value true or false. The default value is false.
Description : A brief summary of the attribute and its usage.
Access attributes in component:
To access an attribute in a component, use expressions as {! v.<Attribute Name>}.
To access an attribute in a controller and helper, use expressions as {v.<Attribute Name>}:
Output:
Note: When we using the attribute in Component, we must use the exclamatory sign. But when we access the attribute in Controller and Helper, we do not use exclamatory sign.