5

I am new to this. Based on our project req. I have to create unique aura:id to support dynamic appending of elements. I researched on this aura:id does not support expression. Two question

  1. Can I use global id like <a id = "{! GlobalId + '_tab' + index}"> where anchor tag will be in an iteration; so that i have a unique id for each anchor tag.

  2. How can i get this unique id; the way i am using right now is

var globalId = component.getGlobalId(); globalId = globalId + "_tab0";

var activeTab = component.find(globalId);

$A.util.addClass(activeTab,'slds-active');

I am getting the global id but how to find an element with global id?

Is this approach right?

2 Answers 2

2

On your lightning component markup - lets say you use iteration so you have the index variable from it, and you define your GlobalId as an attribute:

<aura:attribute name="GlobalId" type="String" access="GLOBAL" description="my id" />
<aura:attribute name="contacts" type="Contact[]" access="GLOBAL" description="list of my standard contacts" />
<aura:iteration items="{!v.contacts}" var="contact" indexVar="index">              
    <a aura:id="{! v.GlobalId + '_tab' + index}"> </a>
</aura:iteration>

Now your controller should get the id correctly

6
  • how to read this in the controller? cmp.get("id generated bye global_tab0")
    – Akash
    Commented Sep 22, 2016 at 13:44
  • your code is correct - var activeTab = component.find(baseGlobalId + '_tab' + index ); where you know already your baseGlobalId and your index. Commented Sep 25, 2016 at 5:42
  • but i am not able to find element i am getting undefined when i am trying to read
    – Akash
    Commented Sep 28, 2016 at 10:17
  • i guess compoent.find only finds aura id:
    – Akash
    Commented Sep 28, 2016 at 10:17
  • 4
    aura id can not be dynamic
    – Akash
    Commented Sep 28, 2016 at 10:18
0

globalId is unique in every component.

<aura:component >
{! globalId}
</aura:component>

2)controller

var id=component.getGlobalId();
console.log(id); // global id like this 345:0 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .