Ng-repeat提交带有生成字段的表单

时间:2015-06-27 13:08:22

标签: javascript angularjs forms validation angularjs-ng-form

我有这个问题,我有一个ng-repeat,其中我添加了一些输入字段,我想将这些输入的值提交给生成的表单 - dosn&t使用ng-model,但是我可以通过表单标记内的输入名称获取。

<li ng-repeat="group in tournament.groups">
    <form novalidate ng-submit="tournament.submit(group.team1, group.team2)">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x">
        </span> - <span>
            <input type="number" name="team2" placeholder="x">
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

3 个答案:

答案 0 :(得分:2)

您应该使用ng-model&amp;绑定输入。 name form的{​​{1}}属性遗失了,因为ng-submit没有被点燃。

当您在form假设name="team"时添加名称属性时,angular会在范围内创建一个新变量&amp;添加有关表格和有效性的所有表格相关信息。每个领域的信息。

<强>标记

<li ng-repeat="group in tournament.groups">
    <form name="team" novalidate ng-submit="tournament.submit(group.team1, group.team2)">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="group.team1"/>
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="group.team2"/>
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

答案 1 :(得分:0)

使用ng-model:

查看:

int main()
{
    enum Country {GERMANY = 1, SPAIN = 2, ITALY = 3};
    cout << "Enter an option: ";
    int fCur{};
    cin >> fCur;

    string str;
    if (fCur == GERMANY)
        str = "Germany";
    else if (fCur == SPAIN)
        str = "Spain";
    else if (fCur == ITALY)
        str = "Italy";
    else
    ;// Handle error


    cout << "You selected " << str << endl;
}
控制器中的

<li ng-repeat="group in tournament.groups">
    <form novalidate ng-submit="submit()">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="group.team1.name">
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="group.team2.name">
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

你会得到你的价值。尝试一下我的代码。

答案 2 :(得分:0)

在您使用的方法中,您将拥有多种形式。我建议您只有一个表单并在表单内迭代如下:

<form name="team" novalidate ng-submit="tournament.submit(group.team1, group.team2)">
    <li ng-repeat="group in tournament.groups">

        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="team1"/>
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="team2"/>
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
   </li>
</form>

并使用'$ index'为每个组更新ng-model(如ng-model =“team {{$ index}}”)

我想这应该可以解决问题。