You need to sign in to do that
Don't have an account?
lightning demo 22
alert not displaying when button is clicked
Hello
I am going through aura:Action and developed a piece of code as follows.
When I click the button I am not able to get the alert message.
Please help me out.
thanks
oliva
I am going through aura:Action and developed a piece of code as follows.
P1Event.evt ---------- <aura:event type="COMPONENT" access="global" description="Event template"> </aura:event> P1Child.cmp ----------- <aura:component> <aura:registerEvent name="handleComponentEvent" type="c.P1Event"/> <lightning:button label="Click Here To Fire The Component Event" onclick="{!c.FireComponentEvent}" aura:id="button1" variant="brand"/> </aura:component> P1ChildControlelr.js -------------------- ({ FireComponentEvent : function(component, event, helper) { //only get the event name var evtName=component.getEvent("handleComponentEvent"); alert(evtName); evtName.fire(); } }) P1Parent.cmp ----------- <aura:component implements="flexipage:availableForAllPageTypes" access="global"> <!--Here handle the component event--> <aura:handler event="c:P1Event" name="myEventInstance" action="{!c.HandleComponent}"/> <c:P1Child aura:id="ClapButton"/> <c:P1Child aura:id="SoundButton"/> <c:P1Child aura:id="DanceButton"/> </aura:component> P1ParentController.js --------------------- ({ HandleComponent : function(component, event,helper) { var buttonclicked = event.getSource().getLocalId(); //find out which button was clicked alert(buttonclicked); if (buttonclicked === 'ClapButton') alert("I clapped"); else if (buttonClicked === 'jumpbutton') alert('I am jumping!'); else alert('I am dancing!'); } })
When I click the button I am not able to get the alert message.
Please help me out.
thanks
oliva
({
HandleComponent : function(component, event) {
var buttonClicked = event.getSource().getLocalId();
var toastEvent = $A.get("e.force:showToast");
if (buttonClicked === 'ClapButton')
{
resultsToast.setParams({
"title": "Saved",
"message": "I clapped!!",
"mode":"sticky"
});
resultsToast.fire();
}
else if (buttonClicked === 'SoundButton')
{
resultsToast.setParams({
"title": "Saved",
"message": "I Made a sound!",
"mode":"sticky"
});
resultsToast.fire();
}
else
alert('I am dancing!');
}
})
But when I click the button clap in dev console then its now showing the appropriate alert.
could u le tme know?
thanks
oliva
Find the attached Toast message code. You are declearing 'toastEvent' variable and using 'resultToast' variable for toast. Add the component to LIghtning page, where you can see it working. Hope this helps.
As I was going through your original question, I found that what are you trying to attain is easily possible. No need to go through the Toast. Your approach was also right. For me its working. One mistake that I was able to find out is in event registration, it should be type="c:P1Event".
Child Component: LightningDS Child Component Controller: LightningDSController Parent Component: Parent Component Controller:
Hope this helps.