Lightning Components: can aura:id be set to an component attribute?
The alternative to this is to dynamically create the component using $A.createComponent and since attribute is accessible via the JavaScript one can assign dynamic values to aura:I'd
Currently the expression syntax is not supported in the aura:id attribute
No, it's not possible to dynamically set aura:id
based on aura:attribute
for any component, it should always point to literal/hardcoded string value.
Below statement is mentioned in the docs.
aura:id doesn't support expressions. You can only assign literal string values to aura:id.
Having aura:id point to Aura Expression, will not throw error when saving the markup.
<div aura:id="{!v.whatever}" /> //No error
But when trying find the div using component.find()
, you won't be able to locate the div(i.e aura:html) it will return undefined
.
component.find(cmp.get("v.whatever")) // returns undefined
It seems to be possible to save an component having markup like
<div aura:id="v.whatever" />
however an usage like this in the controller
cmp.find( cmp.get("v.whatever") )
will come back empty. So my assumption is, that it is not possible by design - even if the markup parser does not complain about it.