获取angularjs中具有相同ng-model的表单字段的所有值

时间:2016-12-14 07:10:09

标签: angularjs

我是angularjs的新手,并使用ng-repeat制作动态表单字段,并且所有字段都具有相同的ng-model

我的代码:

<form name="dataset" class="md-inline-form" novalidate>
     <div ng-repeat="column in columns">
        <input type="hidden" name="column_name" ng-model="dataset.columnName" value="{{column}}">
        <label for="">Column: <strong>{{column}}</strong></label>
        <div layout="column" layout-gt-xs="row">
             <md-input-container flex>
                 <label>Select Type</label>
                 <md-select name="type" ng-model="dataset.type" required>
                      <md-option value="string">String</md-option>
                       <md-option value="numeric">Numeric</md-option>
                  </md-select>
                  <div class="errors" ng-messages="dataset.state.$error">
                   <div ng-message="required">Required</div>
                  </div>
             </md-input-container>
        </div>
    </div>
    <md-button type="submit" ng-click="SavevalidateColumns()" class="md-raised md-accent" aria-label="Submit">Validate Now</md-button>
 </form>

现在我只想在控制器中获取所有动态创建的字段的值。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你必须使用我的代码。

<form name="dataset" class="md-inline-form" novalidate>
     <div ng-repeat="column in columns">
        <input type="hidden" name="column_name" ng-model="dataset.columnName" value="{{column}}">
        <label for="">Column: <strong>{{column}}</strong></label>
        <div layout="column" layout-gt-xs="row">
             <md-input-container flex>
                 <label>Select Type</label>
                 <md-select name="type" ng-model="dataset.type" required>
                      <md-option value="string">String</md-option>
                       <md-option value="numeric">Numeric</md-option>
                  </md-select>
                  <div class="errors" ng-messages="dataset.state.$error">
                   <div ng-message="required">Required</div>
                  </div>
             </md-input-container>
        </div>
    </div>
    <md-button type="submit" ng-click="SavevalidateColumns(dataset)" class="md-raised md-accent" aria-label="Submit">Validate Now</md-button>
 </form>

首先你需要在函数中传递数据。

您的控制器代码:

(function () {
    'use strict';

    angular.module('moduleName', [])
            .controller('myCtrl',
                    function ($scope) {

                        $scope.SavevalidateColumns = function (dataset) {
                            var colName = dataset.columnName;
                            var type = dataset.type;
                            console.log(colName + "::::" + type);
                        };

                    });
})();

您还必须将此模块注入应用程序。