I want to add a lightning component within a component with providing a dynamic name to the component. For example :- if I have 2 components then in javascript I will add component 1 other wise add component 2 in the app based on some condition/criteria.
1 Answer
You can load a Lightning component dynamically using Javascript.
For example:
$A.createComponent(
"c:YourCustomComponentName", //the name of your custom component
{
"yourAttribute1": theValueToPass, //Here are the attributes needed to load your custom component
"yourAttribute2": theValueToPass2
},
function(newComponent, status, errorMessage){ //Your new created component is stored in the newComponent variable
//Here you can add the new component body to your current component
var targetCmp = component.find('theContainer'); //Here you select the component in which you want to add the new created component
var body = targetCmp.get("v.body");
body.push(newComponent);
targetCmp.set("v.body", body);
}
);