[英]Salesforce: How to get field value in aura attribute and use if statement in component
I'm new to Salesforce development, but not new to web development (Angular, React, C#, JS, etc...).我是 Salesforce 开发的新手,但对 web 开发(Angular、React、C#、JS 等)并不陌生。 So it's been very frustrating that I cannot get something so simple as the example below to work.
因此,我无法像下面的示例那样简单地工作,这非常令人沮丧。 I want to display content based upon a condition of a custom fields value:
我想根据自定义字段值的条件显示内容:
<aura:component controller="MyTestController" implements="flexipage:availableForRecordHome,force:appHostable,lightning:actionOverride,force:hasRecordId">
<aura:attribute name="oppty" type="Opportunity" />
<article class="slds-card">
<aura:if isTrue="{!v.oppty.MyCustomField__c == 'hello'}">
Hello
</aura:if>
<lightning:button label="Click Me" onclick="{!c.handleClick}" class="slds-m-top_medium" />
</article>
</aura:component>
In my JS controller, when I console.log()
my attribute oppty
, I get undefined
:在我的 JS controller 中,当我
console.log()
我的属性oppty
时,我得到undefined
:
({
handleClick : function(cmp, event) {
var myOpportunity = cmp.get("v.oppty");
console.log(myOpportunity); // or
console.log(myOpportunity.MyCustomField__c);
}
})
I guess a better question is this: How do I get a value from a field of object Opportunity and bind it to the aura attribute?我想一个更好的问题是:如何从 object Opportunity 的字段中获取值并将其绑定到 aura 属性?
I see you have not included the code for fetching the record and not even having any call to APEX to fetch the data.我看到您没有包含用于获取记录的代码,甚至没有调用 APEX 来获取数据。
There are 2 ways to get the data.有两种获取数据的方法。
<aura:attribute name="recordId" type="String" /> <lightning:recordForm recordId="{.v,recordId}" objectApiName="Opportunity" mode="readonly" fields="Name, MyCustomField__c" />
Using APEX call使用 APEX 调用
Create AuraEnabled method in APEX class and consume in component helper.在 APEX class 中创建 AuraEnabled 方法并在组件助手中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.