Lightning – Create or Place a Component Dynamically.

Your Mark.up

<aura:component>
<div aura:id="YOUR_DIV_ID"/>
</aura:component>

Your Controller.js

DoInit:function(component, event, helper){
$A.createComponent("c:ComponentName",
                      {"recordId": t },
                      function(cmp, status, errorMessage){
                           var divComponent = component.find("YOUR_DIV_ID");
                             if (divComponent.isValid()) {
                                   var body = divComponent.get("v.body");
                                   body =[];
                                   body.push(cmp);
                                   divComponent.set("v.body",body);
                               }
                     }
  );
}

References : https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_dynamic_cmp_async.htm

The syntax is:

$A.createComponent(String type, Object attributes, function callback)

Type—The type of component to create; for example, “ui:button”.
attributes—A map of attributes for the component, including the local Id (aura:id).
callback(cmp, status, errorMessage)—The callback to invoke after the component is created. The callback has three parameters.
cmp—The new component created. This enables you to do something with the new component, such as add it to the body of the component that creates it. If there’s an error, cmp is null.
status—The status of the call. The possible values are SUCCESS, INCOMPLETE, or ERROR. Always check the status is SUCCESS before you try to use the component.
errorMessage—The error message if the status is ERROR.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s