aura:method in
Salesforce
- This enables you to directly call a method in a component’s client-side
controller instead of firing and handling a component event.
- Using <aura:method> simplifies the code needed for a parent component to
call a method on a child component that it contains.
ParentComp:
<aura:component description="parentComponent"
access="global"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes">
<c:childComponent aura:id="childQuestion"/>
<button
type="button" aura:id="saveAndReturn"
class="slds-button" onclick="{!c.save}">Save and return
</button>
</aura:component>
Controller:
({
save :
function(component,helper,event){
var childComponent =
component.find("childQuestion");
childComponent.getScoreMethod('aa','bb','cc');
}
})
childComponent.cmp
<aura:component description="childComponent" access="global">
<aura:method name="getScoreMethod" action="{!c.calcScore}" access="PUBLIC">
<aura:attribute name="contactId" type="String" />
<aura:attribute name="contactName" type="String" />
<aura:attribute name="recordId" type="String" />
</aura:method>
</aura:component>
({
calcScore :
function(component,event,helper){
var args =
event.getParam("arguments");
var contactId
= args.contactId;
var
contactName = args.contactName;
var
recod=args.recordId;
alert('Inside
Calc Score: ' + contactId + ' - ' + contactName+'-'+recod);
}
})
Application:
<aura:application >
<c:ParentComp/>
</aura:application>