0

I've got an array in an aura attribute that I wish to use custom labels for:

<aura:attribute name="options" type="List" default="[{'label': 'Phone', 'value': 'option1'},
                                                    {'label': 'Email', 'value': 'option2'},]"/>

Simply replacing the value in 'label' with my custom value like this:

<aura:attribute name="options" type="List" default="[{'label': '{!$Label.c.Phone}', 'value': 'option1'},
                                                    {'label': '{!$Label.c.Email', 'value': 'option2'},]"/>

Throws an error:

Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}

Any suggestions on how to solve this?

  • are you good with using a server side controller? – RedDevil Jun 18 '18 at 9:37
  • Uhm well, I guess.. I have a few server controllers, just for simpler queries though – erikvm Jun 18 '18 at 9:43
2

why don't you try this?

({
doInit : function(component, event, helper) {
     var defaultOptions = "[{label:$A.get("$Label.c.Phone"),value: 
     'option1' },
     {label: $A.get("$Label.c.Email"), value: 'option2'}]";

    component.set("v. options",defaultOptions);
}
})

Call doInit method on component load. if not you give a try with string concatenate in same. I did once, you need to give a try.

  • Hmm.. not sure how I can put these in the "default" slot in the attribute though – erikvm Jun 18 '18 at 10:31
  • The doInit will load the default values whenever the component gets initialized. All you need to do is call this method on component init like below. '<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>' – SFDC Mafia - VIC Jun 18 '18 at 10:42
  • Oh, didn't mean that, I know init loads automatically, but I was just wondering if I needed to pass that value to default= ..., apparently I didn't, it works! Thanks for your help! – erikvm Jun 18 '18 at 10:54

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.