+ Start a Discussion
Aakanksha Singh 11Aakanksha Singh 11 

Dynamic Lightning Components

Hello Everyone,

I'm working on creating dynamic components, It is working succesfully just for once, and not afterwards. I'm posting my code as well, Please help me with this:
<!--DynamicModalBox(Component)-->
<aura:component >
    <c:DynamicModalBoxButton/>
</aura:component>
 
<!--DynamicModalBoxButton(Component)-->
<aura:component >
    <ltng:require styles="{!$Resource.SLDS +'/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>
    <aura:registerEvent name="openPopup" type="c:DynamicModalBoxEvent"/>
    <aura:handler name="RemoveComponent" action="{!c.removeComponent}" event="c:RemoveComponent"/>
	<div class="static">
    	<button class="slds-button slds-button--neutral" onclick="{!c.getCompnent}">Open Modal</button>
    
        <div aura:id="cmpBody">
            {!v.body}
        </div>
    </div>
</aura:component>


<!--Controller-->
({
	getCompnent: function(cmp,event) {
        //var articleId = cmp.get("v.ArticleId");
        var cmpBody=cmp.find("cmpBody");     
        $A.createComponent(
            "c:DynamicModalBoxPopup",{},
            function(newcomponent){
                //Add the new button to the body array
                if (cmp.isValid()) {
                    var body = cmp.get("v.body");
                    body.push(newcomponent);
                    cmp.set("v.body", body);                    
                }
            }            
        );
        var compEvent = cmp.getEvent("openPopup");
       	compEvent.fire();
    },
    removeComponent:function(cmp,event){
        var cmpBody=cmp.find("cmpBody");
        var comp = event.getParam("comp");
		//alert(comp.toString());
        $A.createComponent(
            comp.toString(),{},
            function(newcomponent){
                //Add the new button to the body array
                if (cmp.isValid()) {
                    var body = cmp.get("v.body");
                    cmpBody.set("v.body",[]);
                }
            }
        );
    },
})
 
<!--DynamicModalBoxPopup(Component)-->
<aura:component >
    <ltng:require styles="{!$Resource.SLDS +'/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>
    <aura:handler name="init" value="{!this}" action="{!c.initialize}"/>
    <aura:handler name="openPopup" action="{!c.initialize}" event="c:DynamicModalBoxEvent"/>
    <aura:registerEvent name="RemoveComponent" type="c:RemoveComponent"/>
	<div class="wk_static">
        <div role="dialog" tabindex="-1" aura:id="Modalbox" aria-labelledby="header43" class="slds-modal ">
            <div class="slds-modal__container">
                <div class="slds-modal__header">
                    <button class="slds-button slds-button--icon-inverse slds-modal__close" onclick="{!c.removeComponent}">
                        <span>
                            <c:SVG class="slds-button__icon slds-button__icon--large" xlinkHref="/resource/SLDS/assets/icons/action-sprite/svg/symbols.svg#close" />
                            <span class="slds-assistive-text">Close</span>
                        </span>                 
                    </button>
                    <h2 id="header43" class="slds-text-heading--medium">Modal Header</h2>
                </div>
                <div class="slds-modal__content slds-p-around--medium">
                    <div>
                        <p>Sit nulla est ex deserunt exercitation anim occaecat. Nostrud ullamco deserunt aute id consequat veniam incididunt duis in sint irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit officia tempor esse quis. Cillum sunt ad dolore
                        quis aute consequat ipsum magna exercitation reprehenderit magna. Tempor cupidatat consequat elit dolor adipisicing.</p>
                        <p>Dolor eiusmod sunt ex incididunt cillum quis nostrud velit duis sit officia. Lorem aliqua enim laboris do dolor eiusmod officia. Mollit incididunt nisi consectetur esse laborum eiusmod pariatur proident. Eiusmod et adipisicing culpa deserunt
                        nostrud ad veniam nulla aute est. Labore esse esse cupidatat amet velit id elit consequat minim ullamco mollit enim excepteur ea.</p>
                    </div>
                </div>
                <div class="slds-modal__footer">
                    <button class="slds-button slds-button--neutral" onclick="{!c.removeComponent}">Close</button>
                    <!--<button class="slds-button slds-button-neutral slds-button-brand">Save</button>-->
                </div>
            </div>
        </div>
        <div class="slds-backdrop " aura:id="MB-Back"></div>
        
    </div>
</aura:component>




<!--Controller-->
({
	initialize:function(cmp,event){        
        var cmpTarget = cmp.find('Modalbox');
       	var cmpBack = cmp.find('MB-Back');
        $A.util.addClass(cmpTarget, 'slds-fade-in-open');
        $A.util.addClass(cmpBack, 'slds-backdrop--open'); 
    },
    removeComponent:function(component, event, helper){
        var compEvent = component.getEvent("RemoveComponent");
        compEvent.setParams({
        	"comp" : component
        });
	    compEvent.fire();
    }
})
 
<!--Events Used-->

<!--RemoveComponent-->
<aura:event type="COMPONENT" description="Event template" >
    <aura:attribute name="comp" type="Aura.Component"/>
</aura:event>

<!--DynamicModalBoxEvent-->
<aura:event type="COMPONENT" description="Event template" />

Regards
Aakanksha Singh
Best Answer chosen by Aakanksha Singh 11
Aakanksha Singh 11Aakanksha Singh 11
Hello Everyone, 
I resolved the issue. Please refer to the link mentioned below:
https://webkul.com/blog/create-dynamic-component/