簡體   English   中英

提交具有許多動態創建的輸入的AngularJS表單

[英]submit AngularJS form with many dynamically created inputs

我在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;
      };


  }]);

您的輸入必須綁定到對象。 您可以使用ng-model指令執行此操作。 看一下這個例子: http : //jsfiddle.net/krvom1ja/

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

假設這是您的表單數據,則將其全部保存在同一位置。 然后,當您提交表單時,您只需獲取$ scope.data。

另外,您的輸入數組是一個實際的數組(請參見鍵b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM