如何以角度方式迭代表单字段?

时间:2017-03-13 18:38:52

标签: javascript angularjs

我有一个包含许多字段的表单,我需要使用某个函数逐个处理。您可以假设我需要将所有字段的值连接成一个字符串。

表单使用ng-model。

<div ng-form="form" ng-submit="submit()" novalidate>
    <input ng-model="ctrl.firstName" required="true">
    <input ng-model="ctrl.lastName" required="true">
    <!-- 30+ more inputs -->
</div>

我想以角度方式做这个,而不是迭代DOM。

1 个答案:

答案 0 :(得分:5)

你可以在角度提交功能上做这样的事情:

for (var field in $scope.ctrl) {
    if ($scope.ctrl.hasOwnProperty(field)) {
        // do stuff
        $scope.ctrl[field] ...
    }
}