Quick answer
Yes — you can use custom labels as the default value for an aura attribute. However, you cannot directly reference one attribute from another in the default attribute of aura:attribute. To set an attribute based on another attribute, initialize it in the component’s controller (init handler) or use binding when the component is embedded.
Why you can’t use another attribute directly in default
The default property on aura:attribute is evaluated as a literal or via supported global value providers (like $Label). The component attribute binding (v. provider) isn’t available at parse-time for another attribute’s default, so default="{!v.otherAttr}" won’t work within the same component declaration.
Use custom labels as default (works)
Custom labels are available through the global value provider $Label. You can set a custom label as the default in markup like this:
<aura:attribute name="greeting" type="String" default="{!$Label.c.My_Greeting_Label}" />
Or read it from controller JS using $A.get():
const greeting = $A.get("$Label.c.My_Greeting_Label");
component.set("v.greeting", greeting);
Set an attribute from another attribute — recommended approaches
Option 1 — initialize in controller (preferred inside the same component):
<aura:attribute name="primaryValue" type="String" default="Default A" />
<aura:attribute name="derivedValue" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
// controller.js
({
doInit : function(component, event, helper) {
const primary = component.get("v.primaryValue");
// compute or re-use as needed
component.set("v.derivedValue", primary + ' (derived)');
}
})
Option 2 — when using a child component, bind the child attribute to the parent attribute. The child will receive the parent value at runtime:
// parent.cmp
<c:childComponent childAttr="{!v.someParentAttr}" />
// child.cmp
<aura:attribute name="childAttr" type="String" />
Option 3 — compute via helper or renderer if you need complex logic or to respond to changes using change handler:
<aura:handler name="change" value="{!v.primaryValue}" action="{!c.onPrimaryChange}" />
Summary & best practices
– Use {!$Label.c.YourLabel} in the default of aura:attribute for custom labels.
– Don’t try to use {!v.otherAttr} as an attribute default inside the same component — instead set dependent values in init or via change handlers.
– Prefer initialization in the controller (init) or binding when composing components for clearer lifecycle and predictable values.
If you want, I can provide a ready-to-deploy sample component demonstrating all options above.








Leave a Reply