javascript - 提交带有许多动态创建的输入的 AngularJS 表单

标签 javascript angularjs forms

我正在 JSON 数组中为每个对象生成一组三个输入。这是一个简单的模型,展示了我如何对两个对象执行此操作。我似乎无法弄清楚如何获取所有输入并以“AngularJS 方式”提交。请注意,对于前两个 number 输入,我选择使用 ng-value 而不是 ng-model,因为前者与 bootstrap 发生了非常难看的冲突。方式。

是否有一些简单的方法可以获取所有输入值并提交它们,就像使用标准表单一样?

HTML:

<form name="editForm" class="form-horizontal" ng-submit="saveEdit()">
<table class="edit_table table table-striped">
        <thead>
          <tr>
            <th class="action_name_cell">Action</th>
            <th class="float_cell">Warning Threshold</th>
            <th class="float_cell">Error Threshold</th>
            <th class="toggle_cell">Enabled</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="row in edit_rows()">
            <td class="action_name_cell">test</td>
            <td class="float_cell">
              <div class="form-group">
                <div class="col-sm-8">
                <input type="number" class="form-control" name="{{row.threshold_id}}_warningDecimal"
                       placeholder="10.0" ng-value="row.warning"
                       ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/"
                       step="0.1" required />
                </div>
              </div>
            </td>
            <td class="float_cell">
              <div class="form-group">
                <div class="col-sm-8">
                <input type="number" class="form-control" name="{{row.threshold_id}}_errorDecimal"
                       placeholder="10.0" ng-value="row.error"
                       ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/"
                       step="0.1" required />
                </div>
              </div>
            </td>
            <td class="toggle_cell">
              <label></label><input name="{{row.threshold_id}}_enabled" type="checkbox" ng-checked="row.enabled" data-toggle="toggle">
            </td>
          </tr>
        </tbody>
      </table>
  <div class="base_button_wrapper">
    <button type="submit" class="btn btn-success">Save</button>
    <button ng-click="cancelEdit()" type="button" class="btn btn-default">Cancel</button>
  </div>
</form>

JS:

angular.module('rtmApp')
  .controller('EditCtrl', ['$scope', '$location', '$window', '$timeout',
    function ($scope, $location) {

      // Change views and show the main view
      $scope.cancelEdit = function() {
        $location.path('/');
      };

      // Save, confirm success, then show the main again
      $scope.saveEdit = function() {
        console.log('Here is the data we are saving...');
        // Let's see if we can see the data we are saving/submitting here:
        console.log("? How do I get all the data ?");
        $location.path('/');
      };


      var dummyEditJSON = [{
                            "threshold_id": 1,
                            "action_id": 1,
                            "action_name": "fast_preview",
                            "site_id": 1,
                            "site_name": "test_site",
                            "warning": 3.5,
                            "error": 5.0,
                            "enabled": true
                          },
                          {
                            "threshold_id": 2,
                            "action_id": 2,
                            "action_name": "bill_cgi",
                            "site_id": 1,
                            "site_name": "test_site",
                            "warning": 2.6,
                            "error": 4.2,
                            "enabled": false
                          }
                        ];
      $scope.edit_rows = function() {
        return dummyEditJSON;
      };


  }]);

最佳答案

您的输入必须绑定(bind)到一个对象。您可以使用 ng-model 指令来做到这一点。看看这个例子:http://jsfiddle.net/krvom1ja/

$scope.data = {
        a: 'a',
      b: [{
        v: 'b'
      }, {
        v: 'c'
      }]
    }

假设这是您的表单数据,您将其全部保存在同一个位置。然后,当您提交表单时,您只需获取 $scope.data 即可。

此外,您的输入数组是一个实际数组(查看键 b)

关于javascript - 提交带有许多动态创建的输入的 AngularJS 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722431/

相关文章:

javascript - 如何使用 svg.js 将点添加到已创建的多边形

javascript - API获取请求

javascript - 我如何获得一个按钮来调用一个 JS 脚本,该脚本删除文本并将其替换为网格中的新文本?

javascript - $watch 不适用于指令值

javascript - 即使字段名为 "submit",也以编程方式提交函数

javascript - jquery UI Tab 不随容器滚动

javascript - javascript中不同类型的函数声明

javascript - 如何使用 Angular 限制下拉菜单仅选择一个值?

php - 使用 PHP、Jquery、AJAX 表单机制和数据库中的数据过滤数据

php - 使用 PHP 向数据库表添加可变数量的字段