2

I am creating a component on click of button using $A.createComponent, which is inside a parent component.

<aura:component>
<aura:attribute name="displayNoResults" type="Boolean" default="false"/>
...
...
<lightning:button label="Search" />
...
...
{!v.body}
</aura:component>

I have 2 questions:

  1. If I click the button 1st time, then I need to call the controller method to create the component using $A.createComponent. But if I click the button 2nd time, do I always have to set the body attribute as null before calling my component creation controller method?
  2. If the value of attribute displayNoResults changes in the parent component, how can I transmit the data of this attribute in the child component?

Edited: One more point. The component which is getting created dynamically contains multiple other components. It is acting as a wrapper of components.

1 Answer 1

3

If I click the button 1st time, then I need to call the controller method to create the component using $A.createComponent. But if I click the button 2nd time, do I always have to set the body attribute as null?

No. Once a component goes out of scope, it will eventually be garbage collected. This happens regardless of if you specify a null value first or not.

If the value of attribute displayNoResults changes in the parent component, how can I transmit the data of this attribute in the child component?

If you use Component#getReference, this will happen automatically without any other special considerations:

$A.createComponent(
  "ns:sometype",
  { 
    displayNoResults: component.getReference("v.otherBoolean")
  },
  function(result) {
    ...
  });

The component which is getting created dynamically contains multiple other components. It is acting as a wrapper of components.

The Lightning Framework will garbage collect everything for you, and Component#getReference is still a valid way to get references. In other words, nothing changes.

You must log in to answer this question.

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