角获取表单元素 [英] Angular get form elements

查看:56
本文介绍了角获取表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从传递给函数的表单中获取元素名称和类.我该怎么办?

I am trying to get element name and class from a form that is passed on to a function. How can i do that?

HTML方面.

   <form name="test">
    <div>
         <input type="text" class="test1" name="test2"/>
    </div>
    <div>
         <input type="text" class="test3" name="test4"/>
    </div>
    </form>
    <div>
         <input type="button" data-ng-click="save(test)" />
    </div>

和Javascript方面

and Javascript side

$scope.save = function(form){
   for(how many elements are there)
       (get elements values)
}

我该怎么做,甚至可以那样做吗?我的目的是在必要时更改类和其他一些属性.

how can I do that, can it even be done like that? My purpose is to change class and some other attributes when it's necessary.

推荐答案

您可以直接使用$scopename(即$scope.test)和表单中的元素(例如)访问$scope中的表单. $scope.test.test2.

You can access the form directly from $scope using its name, i.e. $scope.test, and the elements from the form, e.g. $scope.test.test2.

但是,如果要遍历元素而不必知道元素的单独名称,可以执行以下操作:

However, if you want to loop through the elements without having to know their individual names you can do something like:

angular.forEach($scope.test, function (element, name) {
    if (!name.startsWith('$')) {
        // element is a form element!
    }
});

这篇关于角获取表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆