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
Derrick Koon 7Derrick Koon 7 

Lightning creation of UI components dynamically (programmatically) failed

We created a component with .cmp file with a section below:

    <div aura:id="ElementHere"></div>

Then in the Helper JS class, we want to create ui:inputText control dynamically and place them at the div with aura:id="ElementHere":

    createInputText : function(cmp, textLabel) {
        var componentConfig = {
                "componentDef": "markup://ui:inputText",
                "attributes": {
                    "values": { label:textLabel, size:10, default:"enter text" }
                }
            };
        
           $A.componentService.newComponentAsync(
               this,
               function(newInputText){
                   var divElementHereComponent = cmp.find("ElementHere");
                   var divElementHereBody = divElementHereComponent.get("v.body");
                   divElementHereBody.push(newInputText);
                   divElementHereComponent.set("v.body", divElementHereBody);
               },
               componentConfig
        );
    },

When we ran the app, we got a pop-up that complained that divElementHereComponent does not have get method.

Does anyone know if this is a bug, or we have missed some steps?

Thanks,

Derrick
Gaurav KheterpalGaurav Kheterpal
Can you paste the exact error or provide a screenshot of the error?

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Salesforce Mobile Evangelist| Developer Forums Moderator| Dreamforce Speaker


 
Derrick Koon 7Derrick Koon 7
I think I may know the reason, there is some timing issue.  I might have tried to dynamically create the components when the container had not been fully rendered.  The workaround: Rather than calling my "dynamically create the components" right inside doInit, now I call it only when a button on the container is clicked and all the components are showing up.
Gaurav KheterpalGaurav Kheterpal
Good to know, unless you've some real heavylifting going on, there should not really be a significant delay in doInit() execution.

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Salesforce Mobile Evangelist| Developer Forums Moderator| Dreamforce Speaker