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
brahmanaidubrahmanaidu 

unable to get the data on clicking of a button(Component Event)

ChildComponent
--------------
<aura:component >
    <ui:button label="click me" press="{!c.hanldeClick}"/>
    <aura:registerEvent name="componentevent" type="c:SimpleComponentEvent"/>
</aura:component>

SimpleComponentEvent.evt
------------------------
<aura:event type="component" description="component" >
 <aura:attribute name="message" type="string"/>
    </aura:event>
    
    controller
    ------------
    ({
    hanldeClick : function(component, event, helper) {
        var v1=component.get("componentevent");
        v1.setParams({
            "message":'Welcome to sfdcscenarios.blogspot.com'
        });
        v1.fire();
        
        
    }
})


ParentComponent
---------------
<aura:component >
    <aura:attribute name="EventMessage" type="String"/>
    <aura:handler name="componentevent" event="c:SimpleComponentEvent" action="{!c.hanldeParent}"/>
    <c:ChildComponent/>
    {!v.EventMessage}
</aura:component>




Controller
-----------
({
    handleParent: function(component, event, helper) {
        var message = event.getParam("message"); 
        cmp.set("v.EventMessage", message + 'brahmanaidu');         
    } 
    
    
})
Best Answer chosen by brahmanaidu
sfdcMonkey.comsfdcMonkey.com
hi there is 2 error in your code :

1. var v1 = component.getEvent("componentevent");  [it should be getEvent() not get()]

2. second your parent component handler action name and controller method names are not matching they should be same handleParent

thanks hope it will helps you

 

All Answers

Raj VakatiRaj Vakati
You need to use  component.getEvent("componentevent") insted of component.get . Please refer the below code

ChildComponent 
 
<aura:component >
    <aura:registerEvent name="componentevent" type="c:SimpleComponentEvent"/>

    <ui:button label="click me" press="{!c.hanldeClick}"/>
</aura:component>

({
    hanldeClick : function(component, event, helper) {
        var v1=component.getEvent("componentevent");
        v1.setParams({
            "message":'Welcome to sfdcscenarios.blogspot.com'
        });
        v1.fire();
        
        
    }
})

ParnetCMP
<aura:component >
    <aura:attribute name="EventMessage" type="String"/>
    <aura:handler name="componentevent" event="c:SimpleComponentEvent" action="{!c.hanldeParent}"/>
    <c:ChildComponent/>
    {!v.EventMessage}
</aura:component>

({
    hanldeParent: function(component, event, helper) {
        var message = event.getParam("message"); 
        component.set("v.EventMessage", message + 'brahmanaidu');         
    } ,
    
    
})




 
sfdcMonkey.comsfdcMonkey.com
hi there is 2 error in your code :

1. var v1 = component.getEvent("componentevent");  [it should be getEvent() not get()]

2. second your parent component handler action name and controller method names are not matching they should be same handleParent

thanks hope it will helps you

 
This was selected as the best answer