1

Parent Component :-

<aura:component implements="force:appHostable" >
    <aura:handler name="beerSearchEvent" event="c:SearchEvent" action="{!c.handleBeerSearchEvent}"/>
    <div>
        <c:HeaderComponent tagLine="Beer Explorer" message="For all fun to be..."/>
    </div>
    <c:BeerSearch/>
</aura:component>

Parent Component Controller :-

({
    handleBeerSearchEvent : function(component, event, helper) 
    {
        var searchParam = event.getParam('searchText');
        alert(searchParam);
    }
})

Child Component :-

<aura:component >
    <aura:registerEvent name="beerSearchEvent" type="c:SearchEvent"/>
    <aura:attribute name="issearching" type="Boolean" default="false"/>
    <div class="slds-p-around_x-small">
        <lightning:input aura:id="beerSearchButton" 
                         name="Beer Search" 
                         label="Your favourite beer just a search away..."
                         placeholder="Search Your Favourite Beer Here..."
                         type="search"
                         variant="label-hidden"
                         onchange="{!c.doSearch}"/>
    </div>
</aura:component>

Child Component Controller :-

({
    doSearch: function(component,event,helper)
    {
        var componentEvent = component.getEvent('beerSearchEvent');
        var beerSearchInput = component.find('beerSearchButton').get('v.value');
        componentEvent.setParam({searchText : beerSearchInput});
        componentEvent.fire();       
    }    
});

Event :-

<aura:event type="component" description="Event template">
    <aura:attribute name="searchText" type="string"/>
</aura:event>

When I am running this in an Application, the alert is returning me Undefined but as per the expectation it should alert whatever I am entering in the Lightning : input Search. What i am doing wrong??? Please help me out!!!

2

SecureEvent#setParam expects two parameters, a string and an object:

componentEvent.setParam('searchText', beerSearchInput);

If you want to use object notation, use the plural version, SecureEvent#setParams:

componentEvent.setParams({searchText: beerSearchInput});
| improve this answer | |
  • This worked!!! thanks a lot... Been learning lightning for a while and these are small things that I need to learn and remember... – SFDC Alex Aug 9 at 15:18
  • 1
    @SFDCAlex I always use setParams myself, even if there's only one, that way I don't have to worry about the other syntax. – sfdcfox Aug 9 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.