Activity › Forums › Salesforce® Discussions › how to use $A.create Component for creating an existing component dynamically in Salesforce?
Tagged: Aura Component, Components, Javascript, Object Attributes
-
how to use $A.create Component for creating an existing component dynamically in Salesforce?
Posted by Nikita on August 30, 2019 at 1:35 PMhow to use $A.create Component for creating an existing component dynamically?
Deepak replied 6 years, 9 months ago 2 Members · 1 Reply -
1 Reply
-
Create a component dynamically in your client-side JavaScript code by using the $A.createComponent() method. To create multiple components, use $A.createComponents().$A.createComponent Syntax is looking as shown below.
1
$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
In this example, The client-side controller calls $A.createComponent() to create a ui:inputText with a local ID. The function(newInp, status, errorMessage) callback appends the inputText to the body of c:createComponent.<aura:component >
<lightning:button variant=”brand” label=”Create Component” onclick=”{! c.handleComponent }” />
{!v.body}
</aura:component>({
handleComponent : function(component, event, helper) {
$A.createComponent(
“ui:inputText”,
{
“aura:id”: “inpId”,
“labelClass”:”slds-form-element__label”,
“placeholder”:”Enter Some Text”,
“label”: “Enter some text”,
“class”: “slds-input”
},
function(newInp, status, errorMessage){
if (status === “SUCCESS”) {
var body = component.get(“v.body”);
body.push(newInp);
component.set(“v.body”, body);
}
else if (status === “INCOMPLETE”) {
console.log(“No response from server or client is offline.”)
}
else if (status === “ERROR”) {
console.log(“Error: ” + errorMessage);
}
}
);
},
})-
This reply was modified 6 years, 9 months ago by
Deepak.
-
This reply was modified 6 years, 9 months ago by
Log In to reply.