An attribute can have a type corresponding to a standard object.
Syntax:
<aura:attribute name="AccountObj" type="Account" description="Standard object type attribute"/>
Let's take an example of an Array of the account object.
Step 1:Create Lightning Component.
Step2: Create a Javascript controller.
Step 3: Create an Apex controller.
Syntax:
<aura:attribute name="AccountObj" type="Account" description="Standard object type attribute"/>
Let's take an example of an Array of the account object.
Step 1:Create Lightning Component.
<aura:component
controller="Accountobjcontroller">
<aura:attribute
name="Accountobj" type="Account[]"/>
<lightning:button
label="Fetch Accounts" onclick="{!c.getaccount}"/>
<aura:iteration
var="acc" items="{!v.Accountobj}">
<p>{!acc.Name} </p>
</aura:iteration>
</aura:component>
({
getaccount: function(component){
var action =
component.get('c.getaccounts');
//Calling controller method
action.setCallback(this,
function(response){
var state =
response.getState();
if (state ===
"SUCCESS") {
component.set("v.Accountobj", response.getReturnValue());
//Setting value in account object type attribute
}
});
$A.enqueueAction(action);
}
})
Step 3: Create an Apex controller.
public class Accountobjcontroller{
@AuraEnabled
public static List<Account> getaccounts() {
List<Account> Acc =
[SELECT
Id, Name FROM Account];
return acc;
}
}
Step 4: Create Lightning Application
<aura:application
extends="force:slds">
<c:Firstlightningcomponent/>
</aura:application>
No comments:
Post a Comment