Salesforce Component Attributes

Salesforce Component Attributes enables users to make components more dynamic and flexible. In Apex we declare the variable by declaring its data type. In Aura Components we don’t call them variables, we call them Attributes. Component Attributes are field types that are set on a specific instance of a component and can be referenced from within the component’s markup using an expression syntax as shown below.

Component Attributes Example

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" >
    <aura:attribute name="firstname" type="String" default="Prasanth"/>
    <aura:attribute name="secondname" type="String" default="Kumar"/>
    <div class="container">
        Hello, {!v.firstname + ' ' + v.secondname}
    </div>
</aura:component>

Output

Component attribute names must start with a letter or underscore. They can also contain numbers after the first character. The name must be two or more characters. We cannot use attributes with hyphens in the expression. <aura:attribute> tag is the first thing listed in a component’s markup, as it provides an easy way to read the component’s shape at a glance. Aura: component defines the root of the hierarchy

Access modifier

Access modifier defines whether a component is available globally or just public. Global components can be accessed outside the namespace and public components cannot be accessed outside of the current namespace. Use access attribute of <aura:attribute> to control its scope.

AccessDescription
GlobalThe attribute can be used in any namespace
PublicThe attribute can be used within the same namespace only
PrivateThe attribute can be used only within the container app, interface, component, or event, and can’t be referenced externally.

Component Attributes Data Type

BooleanDouble
DateInteger
Date TimeLong
DecimalString
ObjectList
MapSet

Map: A Map is 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{}.

Example:-

<aura:attribute
 name="SectionLabels"
 Type="Map"
 default="{ a: 'label1', b: 'lable2' }"/>

Set: A Ser is a collection that contains no duplicate elements. The order for set times is not guaranteed. Example: ” 4,5,6″ might be returned as “6,5,4”.

<aura:attribute name="SectionLabels" Type="SET" default="4,5,6"/>

We can also define Salesforce standard objects and custom objects as data types as well as custom apex classes. The Data Type can also be sObject. For instance, we can declare an attribute to of type Contact or a custom Object.

<aura:attribute name="myContacts" Type="Contact"
default="{ 'sObjectType':'Contact', 'Name': ''New Contact }"/>