如何在控制器中访问未知表单? [英] How to access unknown form in controller?

查看:102
本文介绍了如何在控制器中访问未知表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个以上动态生成的表单。我需要迭代控制器中表单的所有错误。我使用模型分配表单名称。

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)
});

当我控制 $ scope.myForm 它应该打印表单对象,但会发生什么呢?它只打印validateForm字符串。

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.validateForm ,因为validateForm是名称你给表格一次 name ={{myForm}}编译:

You ultimately want $scope.validateForm since "validateForm" is the name you give to the form once name="{{myForm}}" is compiled:

可以从 $ 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

这篇关于如何在控制器中访问未知表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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