2

I have a component that invokes another component. It looks like this:

<aura:component >
<c:dummycomponent
    label="Test Amount"
    elements="{!v.elems}"
    data="{!v.amount}"/>
</aura:component>

What I want to do next is when information in the dummycomponent changes a function inside the parent component's helper gets invoked.

I tried putting <aura:attribute name="onChange" type="Aura.Action" description="handles change events"/> inside the dummycomponent and then modify the code above to:

<aura:component >
<c:dummycomponent
    label="Test Amount"
    elements="{!v.elems}"
    data="{!v.amount}"
    onchange="{!v.testfunction}" />
</aura:component>

This did not work and I am not sure what I can try next. Any help will be appreciated, thank you.

5

If the parent is simply concerned about getting notifications on its data changing, you can use an aura:valueChange event in the parent directly:

<aura:component>
  <aura:handler name="change" value="{!v.elems}" action="{!c.valueChanged}" />
  <c:dummycomponent...>
</aura:component>

If you want to do it as an event from the child, then you've got to go down the path of using aura:registerEvent in the child component, which ultimately means you have to fire the change event manually, in which case you can then use the onchange mechanism from your original post.

In your child component:

<aura:registerEvent name="onchange" type="aura:valueChange" />

And in your controller code:

fireChangeEvent: function(component, event, helper) {
  var valueChangeEvent = component.getEvent("onchange");
  valueChangeEvent.setParams(
    // set desired values here
  );
  valueChangeEvent.fire();
}
  • Do you mean the controller of the child component or the parent? What calls the fireChangeEvent function? – Arthlete Dec 13 '16 at 17:16
  • 1
    @Arthlete You'd want to fire the event from the to the parent, if you were using the latter example. – sfdcfox Dec 13 '16 at 17:36
  • I just want to add to this answer that a new EVENT(.evt) file has to be created in order for the <aura:registerEvent> to work. Else it will say that no event under that name exists. – Arthlete Dec 13 '16 at 20:05
1

Lightning component doesn't work that way. You can't invoke parent component's method in a child component. What you need to do here is to use events. You can publish an event at child component and in the parent component listens to respond to that event.

You can refer to this trailhead: https://trailhead.salesforce.com/en/lex_dev_lc_basics/lex_dev_lc_basics_events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.