You need to sign in to do that
Don't have an account?
SAHANA GHOSH
Can anyone help on whats missing in the below code ?
Component :
<aura:component controller="displayColoursClass">
<aura:attribute name="Colours" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.Colours}" var="s">
{!s}
</aura:iteration>
<lightning:button onclick="{!c.getNewList}" label="Update"/>
</aura:component>
Apex class :
public class displayColoursClass {
public final List<String> colourlist;
@AuraEnabled
public List<String> getcolornames(){
colourlist.add('Red');
colourlist.add('Green');
colourlist.add('Blue');
colourlist.add('Yellow');
system.debug('colourlist : '+colourlist);
return colourlist;
}
}
JS Controller :
({
getNewList : function(component, event, helper) {
var action = component.get("c.getcolornames");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var stringItems = response.getReturnValue();
component.set("v.Colours", stringItems);
}
});
$A.enqueueAction(action);
}
})
I am getting the below error when i click on 'Update button'
This page has an error. You might just need to refresh it. Unable to find action 'getcolornames' on the controller of c:displayColours Failing descriptor: {c:displayColours}
<aura:component controller="displayColoursClass">
<aura:attribute name="Colours" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.Colours}" var="s">
{!s}
</aura:iteration>
<lightning:button onclick="{!c.getNewList}" label="Update"/>
</aura:component>
Apex class :
public class displayColoursClass {
public final List<String> colourlist;
@AuraEnabled
public List<String> getcolornames(){
colourlist.add('Red');
colourlist.add('Green');
colourlist.add('Blue');
colourlist.add('Yellow');
system.debug('colourlist : '+colourlist);
return colourlist;
}
}
JS Controller :
({
getNewList : function(component, event, helper) {
var action = component.get("c.getcolornames");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var stringItems = response.getReturnValue();
component.set("v.Colours", stringItems);
}
});
$A.enqueueAction(action);
}
})
I am getting the below error when i click on 'Update button'
This page has an error. You might just need to refresh it. Unable to find action 'getcolornames' on the controller of c:displayColours Failing descriptor: {c:displayColours}
PFB points to solve this error:
- The AuraEnabled methods should alwys be static
- You should give access modifier (public/global) to your lightning component
Please refer below code: