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
Lightning PracticeLightning Practice 

can't able to handle the event from parent to child

Hi All,
Please correct me cannot able to handle event from parent to child.

Registered event in parent as 
<aura:registerEvent name="projectSelected" type="c:project_Phase_holdevent"/>

Controller.js to fire an event
 
var proj=component.find('project').get('v.value');
        var ProjectPhasesEvent = $A.get("e.c:project_Phase_holdevent"); 
        ProjectPhasesEvent.setParams({"project": proj}); 
        console.log('event fired'+proj);
        ProjectPhasesEvent.fire();

Event is firing well but cannot able to handle in child component
 
<aura:handler event="c:project_Phase_holdevent" action="{!c.setSelectedProjectId}"/>

Controller.js in child component
 
setSelectedProjectId : function(component, event, helper) {  
        
        var selectedProject = event.getParam("project"); 
        alert(selectedProject);
}

event
<aura:event type="APPLICATION" >
    <aura:attribute name="project" type="string" /> 
</aura:event>

please suggest what is wrong?on button click am firing child component

Thanks.
Ram 


 
Best Answer chosen by Lightning Practice
Lightning PracticeLightning Practice

Hi Abdul ,
Thanks for the time taken for this issue,
The solution is when we fire application event parent and child component should be in one lightning page.Its Passing now

Thanks ,

Ram .  

All Answers

Abdul KhatriAbdul Khatri
As per my knowledge

You Parent should be handling the event, and you should be firing the event inside the child, meant Register the event inside the child and move the button inside it. Then give a try
Lightning PracticeLightning Practice
Hi Abdul Khatri,

Moving button inside child component doesnot meets my criteria as am doing on choosing Parent record displaying its childs in child Component

Thanks ,
Ram 
Abdul KhatriAbdul Khatri
If your requirement is to show the Parent value inside child then just pass that value from Parent to child through an attribute. Create an attribute in a child component. No need for event handling

<c:childcomponent attributname = 'parent value'
    
Lightning PracticeLightning Practice
Thanks Abdul Khatri 
That doesn't work for my requirement.
Thanks,
Ram
Lightning PracticeLightning Practice
Hi Abdul,
Event is firing and am getting ID too.can you please specify where it is ?
Thanks ,
Ram
Lightning PracticeLightning Practice
Even i tried this Event is firing but it is not handling
ParentCmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	<aura:registerEvent name="projectSelected" type="c:example"/>
    <lightning:button label="Submit" onclick="{!c.clickhere}"/>
</aura:component>
Controller.js
({
	clickhere : function(component, event, helper) {
		var proj='hiii';
        var ProjectPhasesEvent = $A.get("e.c:example"); 
        ProjectPhasesEvent.setParams({'message': proj}); 
        console.log('event fired'+proj);
        ProjectPhasesEvent.fire();
	}
})

Childcmp
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	<aura:attribute name="selectedAccount" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:handler event="c:example" action="{!c.setSelectedProjectId}"/>
</aura:component>

controller.js
({
    
    doInit : function(component, event, helper) {
        		var selectedAccount = component.get("v.selectedAccount");

    },

	setSelectedProjectId : function(component, event, helper) {  
        
        var selectedProject = event.getParam('message'); 
        alert(selectedProject);
}
})

Event
 
<aura:event type="APPLICATION" description="Event template" >
    <aura:attribute name="message" type="string"/>
</aura:event>

Thanks,
Ram​​     
Abdul KhatriAbdul Khatri
Hey Ram, I am not sure why but looking at the example link I provided to you, the only thing you need to do the following in order to make it work
  • Create String attribute in the Child component, name it whatever you want let say you named it "project"
<aura:attribute name="project" type="string" />
 
  • In the Parent Component in the end place this code
    <div>                   
        <c:ChildComponent project="link" />
    </div>
</aura:component>
Try now it should fire the Event

This seems little funcky and seems like a bug on the salesforce end but it work. I guess this way telling linking the parent component to child from the handling perpective which automatically happens vice versa.

Let me know if this worked.
Lightning PracticeLightning Practice

Hi Abdul ,
Thanks for the time taken for this issue,
The solution is when we fire application event parent and child component should be in one lightning page.Its Passing now

Thanks ,

Ram .  

This was selected as the best answer