0

I would like to dynamically create components from other component. The thing is that, if any of those child components use events, I'd like to create the needed aura:attributes to store the info.

Anyone know if I can create it with $A.createComponent ? Any example?

Thanks in advance!

Component

<aura:component >
     {!v.customParams} 
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <!-- BODY -->
    <div aura:id="modal" role="dialog" tabindex="-1" class="slds-modal slds-fade-in-open">
        <div class="slds-modal__container">
        </div>
    </div> 
</aura:component >

Javascript

doInit : function(component){
    var attributes = { "name" : 'msg',
                      "type" : 'String',
                      "default" : 'Holaaa'};

    $A.createComponent('aura:attribute',
                       attributes, 
                       function(newCmp, status, errorMessage){
                           console.log("Status : " , status)
                           console.log("newCmp: ", newCmp)
                           console.log(component.get("v.customParams"))
                           if (status === "SUCCESS") 
                           {
                              component.set("v.customParams", newCmp)
                           }else{
                               console.log(errorMessage)
                           }
                       })
},

In the browser console, it shows:

Status = ERROR

newCmp: null

undefined

An internal server error has occurred. Error ID: 1834334539-240 (-488974888)

3
  • 1
    You're trying to set attributes on the component as it's created? Maybe you could show some code that demonstrates what you've tried so far? Even if it's not correct, seeing code might help us solve your question better.
    – sfdcfox
    Feb 25, 2018 at 20:51
  • @sfdcfox now you can see the code Feb 25, 2018 at 21:12
  • 1
    Nope, you can't define attributes this way. Whatever problem you're trying to solve, you'll need to do it using already-defined attributes. This is almost certainly an x-y problem; ask about your original problem so we can help you.
    – sfdcfox
    Feb 25, 2018 at 21:40

1 Answer 1

1

Surely the only reason for attributes is to allow the framework to render them? In which case you'll need to specifically add them in anyway to the .cmp.

If you're just using them to store and manipulate in the controller javascript, you can just have one, specified attribute that is of type Object, and takes a Javascript object of any shape you're trying to pass into it.

You must log in to answer this question.

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