As far as I can tell, Lightning components don't support that dynamic reference syntax.
You could consider dynamically generating the content of this component with the component controller:
Component
<aura:component controller="GetAFewContacts">
<aura:attribute name="contacts" type="Contact[]" />
<aura:attribute name="fieldNames" type="String[]" default='["Id","Name"]'/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
{!v.body}
</aura:component>
Client-side Controller
({
doInit : function(component, event, helper) {
var action = component.get("c.getContacts");
action.setCallback(this,function(response){
if (response.getState() === "SUCCESS"){
console.log(response.getReturnValue());
component.set("{!v.contacts}",response.getReturnValue());
var contacts = component.get("{!v.contacts}");
var fieldNames = component.get("{!v.fieldNames}");
var NewBody = "";
for (var i=0; i<contacts.length; i++){
for (var ii=0; ii<fieldNames.length; ii++){
NewBody += contacts[i][fieldNames[ii]];
}
NewBody += "\n";
}
console.log(NewBody);
$A.createComponent(
"ui:outputText",
{
"value" : NewBody
},
function(newComponent){
component.set("v.body",newComponent);
}
)
}
}
);
$A.enqueueAction(action);
}
})
Server-side Controller
public class GetAFewContacts {
@AuraEnabled
public static list<Contact> getContacts(){
return [SELECT Id, Name FROM Contact LIMIT 10];
}
}
Or you could use a second component that sets the value through the component's renderer:
Component
<aura:component controller="GetAFewContacts">
<aura:attribute name="contacts" type="Contact[]" />
<aura:attribute name="fieldNames" type="String[]" default='["Id","Name"]'/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:iteration items="{!v.contacts}" var="contact" >
<aura:iteration items="{!v.fieldNames}" var="fieldName">
<analysis:ContactAndFieldName contact="{!contact}" fieldName="{!fieldName}" />
</aura:iteration>
</aura:iteration>
</aura:component>
Client-side Controller
({
doInit : function(component, event, helper) {
var action = component.get("c.getContacts");
action.setCallback(this,function(response){
if (response.getState() === "SUCCESS"){
component.set("{!v.contacts}",response.getReturnValue());
}
}
);
$A.enqueueAction(action);
}
})
Server-side Controller
public class GetAFewContacts {
@AuraEnabled
public static list<Contact> getContacts(){
return [SELECT Id, Name FROM Contact LIMIT 10];
}
}
Component -- ContactAndFieldName
<aura:component >
<aura:attribute name="contact" type="Contact" />
<aura:attribute name="fieldName" type="String" />
<ui:outputText aura:Id="outputTextId" />
</aura:component>
ContactAndFieldNameRenderer
({
render : function(component, helper) {
var ret = this.superRender();
var Contact = component.get('v.contact');
var FieldName = component.get('v.fieldName');
var outputText = component.find("outputTextId");
outputText.set("v.value",Contact[FieldName]);
return ret;
},
})
If an init event is used instead of putting the javascript in the renderer:
Component -- ContactAndFieldName
<aura:component >
<aura:attribute name="contact" type="Contact" />
<aura:attribute name="fieldName" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<ui:outputText aura:Id="outputTextId" />
</aura:component>
ContactAndFieldNameController
({
doInit : function(component, helper) {
var Contact = component.get('v.contact');
var FieldName = component.get('v.fieldName');
var outputText = component.find("outputTextId");
outputText.set("v.value",Contact[FieldName]);
}
})
(no custom renderer, and everything else is the same)
Trying it on my end... something funky definitely happening there, having to do with binding a numeric value to that text field. Here is my stupid hack of a fix:
<aura:attribute name="dataProvider" type="Map[]" access="global"
default="[{name: 'Tom'}, {name: 'Bob'}, {name: 'Tim'}]" />
<ui:inputSelect>
<aura:iteration items="{!v.dataProvider}" var="dataItem" indexVar="dataIndex">
<ui:inputSelectOption label="{!dataItem.name}" text="{!''+dataIndex}" />
</aura:iteration>
</ui:inputSelect>
Adding '' + the index ensures it treats it as a string and that seems to unbreak the select list.
Note also that you can bind a Map[] directly to the items
property of ui:inputSelect
. For example:
<aura:attribute name="items" type="Map[]" access="global"
default="[{label: 'Tom', value:0}, {label: 'Bob', value:1}, {label: 'Tim', value:2}]" />
<ui:inputSelect options="{!v.items}" />
Best Answer
You need to call the DoInit handler. Rename your javascript from "getMyObjects" to "doInit."
Component:
Controller:
As a best practice, you should not name your client side functions the same as your apex controller. If you do, you could end up recursively calling the controller which will throw a big error.
Please also note that javascript is case sensitive. Your set of "MyObjects" was spelled as "myObjects" and would have thrown an error.