简体   繁体   中英

How to get values from dynamically created lightning Components?

I am developing an Lightning application where i am creating components dynamically. As of now code is working fine and gives no error. But my question is how we get values from it.

Let's say this is parent controller class ,

clickMoreFilter: function(cmp, event, helper) {
       $A.createComponent(
        "c:AccountDynamicForm",
        {
            "aura:id": "findableAuraId",
            "label": "Press Me",
            "press": cmp.getReference("c.clickCreateExpense")
        },

        function(newButton, status, errorMessage){
            //Add the new button to the body array
            if (status === "SUCCESS") {
                var body = cmp.get("v.body");
                var newb = newButton.getElement('levels3');
                console.log('checkCmp'+newb);
                body.push(newButton);
                cmp.set("v.body", body);
            }
            else if (status === "INCOMPLETE") {
                alert("No response from server or client is offline.")
                // Show offline error
            }
                else if (status === "ERROR") {
                    alert("Error: " + errorMessage);
                    // Show error message
                }

        }
    );
},

and this is the dynamic component which i am creating :

<div aria-labelledby="newexpenseform">
    <!-- CREATE NEW EXPENSE FORM -->
    <br></br>
    <form class="slds-form--inline">
        <div class="slds-form-element slds-is-required">
            <div class="slds-form-element__control">
                <ui:inputSelect aura:id="accIndustry" class="slds-select"/>
            </div>

        </div>

        <div class="slds-form-element slds-is-required">
            <div class="slds-form-element__control">
                <ui:inputSelect aura:id="levels3"  class="slds-input">
                    <aura:iteration items="{!v.contactLevel1}" var="level">
                        <ui:inputSelectOption text="{!level}" label="{!level}"/>
                    </aura:iteration>
                </ui:inputSelect>
            </div>
        </div>

        <div class="slds-form-element">
            <div class="slds-form-element__control" onfocusout="{!c.clickCreateExpense}">
                <ui:inputText aura:id="inputValue" 
                              class="slds-input"
                              labelClass="slds-form-element__label"
                              />
            </div>
        </div>
    </form>
    <!-- / CREATE NEW EXPENSE FORM -->
</div>

Now how to get the values from dynamic component to parent component? Please help me out in this. Thanks in advance.

I think it might sound better to post such questions to SSE .

Also I would suggest to use Lightning Events. Using Lightning events you can define an event to fire inside of a child component and handle it in parent component disregarding if child component was created dynamically or not.

I have described a event creation and subscribing in my post on my blog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM