[SalesForce] How to pass event attribute value to js controller in lightning

I am doing trailhead task of handling event through client side controller in lightning components.I need to set phone number through one component and it should get display through another component. My value is setting an attribute of event but it is not getting to another component. Can anyone please suggest where I am getting wrong?Following is my code:-

—– Event——

<aura:event type="APPLICATION" >
    <aura:attribute name="phone" type="String"/>
</aura:event> 

—–Component that fires the event:-PhoneNumberInput.cmp—

<aura:component >
    <aura:registerEvent name="PhoneNumberEvent" type="c:PhoneNumberEvent"/>
    <ui:inputPhone aura:id="phone" label="phone"/>
    <ui:button label="Show Phone" press="{!c.showPhone}"/>
</aura:component>

—–js controller of Component that fires the event:-PhoneNumberInput.js—-

({
    showPhone : function(component, event, helper) {
        console.log("Inside input controller");
        var phone = component.find("phone").get("v.value");
        console.log("Phone:-"+phone);
        alert('---1----'+phone);
        $A.get("e.c:PhoneNumberEvent").setParams({
            "phone" : phone
          }).fire();
        }
})

—–Component that displays the phone value:-PhoneNumberOutput.cmp—

<aura:component >
    <aura:attribute name="number" type="String" default="No Phone Number"/>
    <aura:handler event="c:PhoneNumberEvent" action="{!c.outputPhone}"/>
    <ui:outputText aura:id="phone" value="{!v.number}"/>
   </aura:component>

——–js controller of Component that displays the phone value:-PhoneNumberOutput.js——

({
    outputPhone : function(component, event, helper) {
        console.log("Inside Output Controller");
       var phone = event.getParams("phone");
       component.set("v.number",phone);
            }
})

Best Answer

In PhoneNumberOutputController.js change event.getParams("phone") to event.getParam("phone"). event.getParams() returns an object, so you could also use event.getParams().phone.

Related Topic