简体   繁体   English

如何在控制器中访问未知表格?

[英]How to access unknown form in controller?

I have more than three dynamically generated forms. 我有三种以上动态生成的表格。 I need to iterate all the errors of the forms in controller. 我需要迭代控制器中表单的所有错误。 I am assigning form names by using models. 我通过使用模型来分配表单名称。

<form name="{{myForm}}" novalidate>     
<input type="text" ng-model="username" name="username" required/>
<span ng-show="(submit && myForm.username.$error.required)">
    <span>Required</span>
</span>

angular.module("myApp",[]).controller("myCtrl",function($scope) {
$scope.myForm= "validateForm";      
console.log("form" + $scope.myForm)
});

What I want is when I console $scope.myForm it should print the form object but what happens is it just prints the "validateForm" string. 我想要的是当我操作$scope.myForm它应该打印表单对象,但是发生的是,它只打印了"validateForm"字符串。

You ultimately want $scope.validateForm since "validateForm" is the name you give to the form once name="{{myForm}}" is compiled: 您最终要使用$scope.validateForm因为一旦编译了name="{{myForm}}"之后, "validateForm"就是您赋予表单的name="{{myForm}}"

Can get it from $scope[$scope.myForm] 可以从$scope[$scope.myForm]

Note that this won't be available in controller until after the view is compiled first time and the validation object is created during the compile process 请注意,只有在第一次编译视图并在编译过程中创建验证对象之后,该控件才在控制器中可用

All you are currently doing is logging a primitive that you assigned on the previous line 您当前正在执行的操作是记录您在上一行中分配的基元

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

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