function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nethra RaghupathyNethra Raghupathy 

Pass attribute in $A.createComponent()

Hi,

I'm calling a existing component using create component and trying to pass attribute for it.
oneController.js
var id='25978371';
        $A.createComponent(
            "c:InDetails",
            {
                oppId: id,
            },
            function(newCmp){
                    component.set("v.currentContent", newCmp);
            }
        );
InDetails.cmp
<aura:attribute name="oppId" type="String"/>
InDetailsController.js
console.log(component.get("v.oppId"))

Indeatails components gets created in component one but console.log of oppId gives me undefined. I couldn't pass the attribute from one component to another.  
 
Aleksei KosovAleksei Kosov
I used your code and it works. oppId outputs. Show me the cmp and js of the two components?
Nethra RaghupathyNethra Raghupathy
Hi Aleksei,

Adding along the complete code.

one.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="Controller">
    
    <aura:attribute name="body" type="Object" />
    <aura:attribute name="oppId" type="String" />
    <aura:attribute name="currentContent" type="String" />
    
    <aura:handler name="init" value="{! this }" action="{! c.init }" />
    
    <div>             
            {! v.currentContent } 
    </div>
    
</aura:component>

InDetails.cmp:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="Controller">

	  <aura:attribute name="interviewData" type="Object"/>
    <aura:attribute name="oppId" type="String"/>
    <aura:attribute name="interviewId" type="String"/>
    <aura:attribute name="interviewName" type="String"/>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    
  <div class="slds-col" style="margin:10px">
      
    </div>
    
 
</aura:component>
IndetailsController.js:
doInit : function(component, event, helper) {
       console.log(component.get("v.oppId"))
}


 
Aleksei KosovAleksei Kosov

HI Nethra Raghupathy,

It works too. I tested these components on a custom app
 

<aura:application description="test" extends="force:slds">
    <c:One/>
</aura:application>

Where Did you test theirs?

Nethra RaghupathyNethra Raghupathy
So I have a vfp which calls this component,
<apex:page sidebar="false" showheader="false">
    <html style="height:100%">        
        <body style="height:100%"> 
            
            <apex:includeLightning />   
            <div id="userContainer" />
            
            <script>
            $Lightning.use("c:BFClassicApp", function() {
                $Lightning.createComponent("c:one",
                                           {
                                               
                                           },
                                           "userContainer",
                                           function(cmp) {
                                           });
            });
            </script>
        </body>
    </html> 
</apex:page>

BFClassicApp.app
<aura:application access="GLOBAL" extends="ltng:outApp">
    <aura:dependency resource="c:one"/>
</aura:application>

 
rambabu nrambabu n
Hi Nethra,

I used your code and it works for me too also.

Another way to  pass attributes from one component to another component.
<c:InDetails oppId="{!v.oppId}" interviewName="Technical"/>

Please try below code,
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="Controller">
    
    <aura:attribute name="body" type="Object" />
    <aura:attribute name="oppId" type="String" default="25978371"/>
    <aura:attribute name="currentContent" type="String" />
    
    <aura:handler name="init" value="{! this }" action="{! c.init }" />
    
    <div>             
          <c:InDetails oppId="{!v.oppId}" />
    </div>
    
</aura:component>


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Rambabu


 
Nethra RaghupathyNethra Raghupathy
Hi rambabu,

I can't define child component because which component needs to be rendered is dynamic and Its based on the API callback.

The problem here is $A.createComponent is inside API callback and it loses its attribute memory during the callback. 

Started a seperate thread for this  https://developer.salesforce.com/forums/ForumsMain?id=9062I000000g6gbQAA