An attribute can have a type corresponding to a custom object.
Syntax:
<aura:attribute name="empobj" type="employees__c[]"/>
Let's take an example of an array of custom object employees.
STEP 1: Create a Lightning Component.
STEP 2: Create a Javascript controller.
    
        
Syntax:
<aura:attribute name="empobj" type="employees__c[]"/>
Let's take an example of an array of custom object employees.
STEP 1: Create a Lightning Component.
<aura:component controller="Employeeobjcontroller">  
    <aura:attribute name="empobj" type="employees__c[]"/>  
    <lightning:button label="Fetch Employee details" onclick="{!c.getemp}"/>
    <aura:iteration var="acc" items="{!v.empobj}">  
        <p>{!acc.Name} </p>
    </aura:iteration>
</aura:component>
({
    getemp: function(component){
        var action = component.get('c.getempdetails');
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.empobj", response.getReturnValue()); 
            }
        });
     $A.enqueueAction(action);
    }
})
Step 3: Create an Apex controller.
public
class Employeeobjcontroller{
    @AuraEnabled
    public static
List<employees__c> getempdetails() {
       
List<employees__c> Acc =
         
      [SELECT Id, Name  FROM employees__c];
        return
acc;
    }
    }
STEP 4: Create lightning application.
Create lightning application.
<aura:application extends="force:slds">
    <c:Firstlightningcomponent/>
</aura:application>
 
In Step:3 class name should be "Employeeobjcontroller" not "Accountobjcontroller".
ReplyDeleteThanks, Updated
Delete