简体   繁体   中英

ng-form to submit dynamically created form without specific ng-model

Edited the code to ask the question better
And thanks for all the help from everyone so far!
I have some dynamically created input boxes that do not have a fix number of inputs and would like to submit them as you go....
how could I pass inputs from the input text boxes from the dynamically created form?
Hopefully my question make sense?
the form I am trying to submit is as follow:
how do I pass values into the create()? passing a currently does not work... and referencing $scope.a gets 'undefined'... :(
Please help thank you!

<form ng-submit="create(a)">
    <div class="module-head text-center">
        <b class="lead">{{ module.heading }}</b>
        <input class="btn" type="submit" id="submit" value="+"/>
    </div>
    <div class="module-body">
        <div class="control-group" ng-repeat="field in module.fields">
            <label class="control-label">{{field.name}}</label>
            <div class="controls">
                <input type="text" name="fields"
                 ng-model="a[$index]" ng-init="a[$index]=field.data">
            </div>
          </div>
      </div>
</form>

How about something like this?

<form ng-submit="create()">
    <input class="btn" type="submit" id="submit" value="+"/>
    <div ng-repeat="item in items">
        <input type="text" name="fields" ng-model="itemArray[$index]">
    </div>
</form>

When you submit this, the itemArray will have all the model values of the input fields.

To assign the original values to the input boxes.

<input type="text" name="fields" ng-model="a[$index]" ng-init="a[$index]=item"/>

I am not sure if I get your question, but if your concern is to access the ng-model values in controller you can create an array line $scope.arr=[];

And in your form use it like

<div ng-repeat="item in items">
    <input type="text" name="fields" ng-model="arr[item]"/>
</div>

In this way $scope.arr will have all your input values.

See fiddle: http://jsfiddle.net/Sourabh_/HB7LU/13274/

You would try doing as:

<form ng-submit="create()">
    <input class="btn" type="submit" id="submit" value="+"/>
    <div ng-repeat="item in items">
        <input type="text" name="fields" ng-model="items[$index]">
    </div>
</form>

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