简体   繁体   English

我如何以角度方式遍历表单字段?

[英]How do I iterate over form fields angular-way?

I've got a form containing many fields, which I need to process one-by-one using some function. 我有一个包含许多字段的表单,我需要使用某些函数来对其进行逐一处理。 You can assume I need to concatenate all fields' values into a string. 您可以假设我需要将所有字段的值连接到一个字符串中。

The form uses ng-model. 该表格使用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>

I would like to do this angular-way, without iterating over the DOM. 我想这样做,而不需要遍历DOM。

You could do something like this on angular submit function: 您可以在角度提交功能上执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM