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
Aakanksha SinghAakanksha Singh 

Removing the component dynamically

Hi everyone,
i've to remove the component dynamically, here is my code:
<pre>
<!--DynamicComponent-->
<aura:component >
    <ltng:require styles="{!$Resource.SLDS +
             '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>
    <aura:handler name="removeComponent" action="{!c.removeComponent}" event="c:TestEvent"/>    
    <div class="wk_static">
        <button class="slds-button slds-button--neutral" onclick="{!c.getCompnent}">Open Modal</button>
        <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:VehicleSVG 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>
                {!v.body}        
                <div class="slds-modal__footer">
                    <button class="slds-button slds-button--neutral" onclick="{!c.removeComponent}">Cancel</button>
                    <button class="slds-button slds-button--neutral slds-button--brand" onclick="{!c.removeComponent}">Save</button>
                </div>
            </div>
        </div>
        <div class="slds-backdrop " aura:id="MB-Back"></div>
    </div>
</aura:component>
</pre>
Controller:
<pre>
({
    getCompnent: function(cmp,event) {
        $A.createComponent(
            "c:TestComponent",{},
            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 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(cmp,event){
        $A.createComponent(
            "c:TestComponent",{},
            function(newcomponent){
                //Add the new button to the body array
                if (cmp.isValid()) {
                    var body = cmp.get("v.body");
                    cmp.set("v.body",[]);
                }
            }
        );
        var cmpTarget = cmp.find('Modalbox');
           var cmpBack = cmp.find('MB-Back');
        $A.util.removeClass(cmpBack,'slds-backdrop--open');
        $A.util.removeClass(cmpTarget, 'slds-fade-in-open');
    }
})
</pre>
Test component:
<pre>
<aura:component >
     <ltng:require styles="{!$Resource.SLDS +
             '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>
    <aura:registerEvent name="removeComponent" type="c:TestEvent"/>
    <div class="wk_static">
        <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>
                <button class="slds-button slds-button--neutral" onclick="{!c.removeComponent}">Cancel</button><!--I've to remove component through this button-->
            </div>
        </div>
    </div>
</aura:component>
</pre>
controller:
<pre>
({
    removeComponent : function(component, event, helper) {
        var evt = component.getEvent("removeComponent");
        evt.fire();
    }
})
</pre>
Can anyone help,
Thanx in advance.
NagendraNagendra (Salesforce Developers) 
Hi Akanksha,

Please refer to the post below which helps you accelerate further.
http://salesforce.stackexchange.com/questions/82617/how-can-i-remove-a-lightning-component-from-the-dom

Please let us know if this helps.

Best Regards,
Nagendra.P
Aakanksha SinghAakanksha Singh
Hi Nagendra,
Thanks for replying, bt the link you are referening, is already implemented in the code. My question is, how can we implement the functionality when the button itself is inside {!v.body}.
 
Srini D 18Srini D 18
Hi Aakanksha,

Did you get the solution fo above requirement?

Thanks,
Srinivas