3

We have a VF page with Lightning components on it. we need to read a User input (i.e. Apex control value) inside my lightning component JS controller.

It seems that,

document.getElementById("apexCntId").value

is not available now. What are the options available for us to read Apex page element from JS controller?

Can we update some <aura:attribute> inside lightning component which is created using $Lightning.createComponent()? I couldn't find a doc to go with this approach.

2
  • Just for initialization, or later on in the lifecycle?
    – sfdcfox
    Commented May 5, 2017 at 14:41
  • @sfdcfox it's to be used later in component. Commented May 6, 2017 at 14:21

2 Answers 2

3

You need to fire an event at your component. I don't have time to actually test this, but something like the following should work:

<!-- Visualforce snippet -->

<script>
function sendDataToLightning(el) {
  var evt = ($A || window.opener.$A).get("e.c:updateComponentValue");
  evt.setParams( { value: el.value } );
  evt.fireEvent();
}
</script>
<input type="text" onchange="sendDataToLightning(this);" />

<!-- Lightning component snippet -->

<aura:handler event="c:updateComponentValue" action="{!c.updateValue}" />

Note that what I'm implying is that you must communicate across the Visualforce/Lightning barrier via events. You can listen for Lightning Events in Visualforce, and you can listen for Lightning Events initiated outside of Lightning to update your component's state. See Firing Lightning Events from Non-Lightning Code as an example.

3
  • thanks, but unfortunately handler seems not capturing the event. I doubt it's because they are not being parent-child event propagation chain. Commented May 6, 2017 at 14:27
  • 1
    @PasanEeriyagama it needs to be an application event. Component events likely won't work.
    – sfdcfox
    Commented May 6, 2017 at 14:28
  • Thank you @sffdcfox it is APPLICATION type. unfortunately It is not getting to handler. Event markup: <aura:event type="APPLICATION" description="Event template"> </aura:event> Apex page Javascript (this code executed with no errors): var evt = ($A || window.opener.$A).get("e.c:testEvent"); evt.fire(); Component markup: <aura:handler name="testEvent" event="c:testEvent" action="{!c.handleEvt}" /> Component JS controller: handleEvt: function(component, event, helper) { console.log('called'); alert('test'); } Seems Event is not being captured in the handler. Commented May 7, 2017 at 2:31
0

Your Lightning component's controller operates in a separate namespace than your visualforce page does and won't be accessible directly from your page without passing values from the Lightning component's controller by either passing them from the controller to an Apex controller or by setting Attributes within the Component's Controller that apply to variables used on your page.

If you define <aura:attributes> in your component, then use set("v.value", somevar.value) in your component's controller (or a helper), that will set the attribute's value for all components within the namespace which should also pass them into the visualforce page just as it would in a Lightning page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .