简体   繁体   English

如何在angularjs中访问控制器中的多个表单?

[英]how to access Multiple form in controller in angularjs?

I have to do form validation for multiple forms which have to be created dynamically. 我必须为必须动态创建的多个表单进行表单验证。 I have created the forms dynamically by using ng-repeat but I am not able to access that form in controller. 我已经使用ng-repeat动态创建了表单,但我无法在控制器中访问该表单。

Please check the code: 请检查代码:

 <button ng-click="navigate()">Next</button>
   <div ng-repeat="service in services">
     <ng-form name="mainform">
        <div ng-repeat="spec in service.products">
           <ng-form name="subform">
               <input type="text" name="{{spec.name}}" ng-model="spec.value">
               <span ng-show="subform[spec.name].$invalid">please enter</span>
           </ng-form>
        </div>
    </ng-form> 
  </div >

It is working fine, but I need to check whether at least one of mainform's subforms is valid or not after clicked on next button so I have tried to access this in controller like this: 它工作正常,但我需要检查主机的子窗体中是否有一个是有效的,然后单击下一个按钮,所以我试图在控制器中访问它,如下所示:

 $scope.navigate=function(){
     console.log($scope.mainform.subform);
     console.log($scope.subform);
 }

but I am getting undefined for both console logs. 但我的两个控制台日志都undefined How can I access multiple dynamically created forms in the controller? 如何在控制器中访问多个动态创建的表单?

What exactly do you want to do with forms in controller? 你想用控制器中的表格做什么?

Looks like you don't need it. 看起来你不需要它。

You have forms hierarchy. 您有表单层次结构。 Pseudocode: 伪代码:

ng-form mainForm
  ng-form subFormOne
  ng-form subFormTwo

If subFormOne or subFormTwo is invalid then mainForm will be invalid too. 如果subFormOnesubFormTwo无效,那么mainForm也将无效。 If all sub forms are valid then mainForm will be valid too. 如果所有子表单都有效,那么mainForm也是有效的。 You will check only mainForm.$valid in your code. 您只需检查代码中的mainForm.$valid

If you need to style it somehow you I recommend you to use css there. 如果您需要以某种方式设置它,我建议您在那里使用CSS。 ngForm directive adds classes to the element. ngForm指令将类添加到元素中。 You can use .ng-form selector in css to add styles to your form. 您可以在css中使用.ng-form选择器为表单添加样式。 For example: 例如:

.ng-form.ng-invalid {
  background-color: #ff0000;
} 

Here is plunkr example . 这是plunkr的例子

The logs show undefined because because the ng-repeat directive creates child scopes and the forms are put on those child scopes. 日志显示undefined ,因为ng-repeat指令创建子范围,表单放在这些子范围上。

Create a form at the top level, above the ng-repeat : ng-repeat上方的顶层创建一个表单:

<button ng-click="navigate()">Next</button>

<!-- ADD top form above ng-repeat -->
<ng-form name=top>
    <div ng-repeat="service in services">
        <ng-form name="mainform_{{$index}}">
            <div ng-repeat="spec in service.products">
                 <ng-form name="subform_{{$index}}">
                     <input name="{{spec.name}}" ng-model="spec.value">
                 </ng-form>
            </div>
        </ng-form> 
    </div>
</ng-form>

Then forms inside the ng-repeat will be visible: 然后可以看到ng-repeat内的表格:

$scope.navigate=function(){
     console.log($scope.top);
     console.log($scope.top.mainform_0);
     console.log($scope.top.mainform_0.subform_0);
};

The DEMO on PLNKR PLNKR上DEMO

I would use $index from ng-repeat or something makes it unique. 我会使用ng-repeat中的$ index或者某些东西让它变得独一无二。

UPDATE: 12/6/2016 Based on the comment. 更新:12/6/2016根据评论。

plnkr of deeply nested dynamic forms and validation plnkr深度嵌套的动态表单和验证

<button ng-click="navigate()">Next</button>
<div class="outer" ng-repeat="service in services" ng-init="serviceSuffix=$index;serviceForm = 'form' +serviceSuffix">
  <h2>{{service.serviceName}} id= {{service.id}} {{serviceForm}}</h2>

  <ng-form name="{{serviceForm}}">
    <button ng-click="isValidForm(serviceSuffix)">Is Outer valid</button>
    <div class="inner" ng-repeat="spec in service.products" ng-init="specSuffix = serviceSuffix+'_'+$index; specForm = 'form' +specSuffix; ">
      <h2>{{spec.specName}} id={{spec.id}} {{specForm}}</h2>
      <ng-form name="{{specForm}}">
        <input required type="text" name="{{spec.specName}}" ng-model="spec.value">
        <span ng-show="this[specForm][spec.specName].$invalid">please enter</span>
        <button ng-click="isValidForm(specSuffix)">Is Inner valid</button>
      </ng-form>
    </div>
  </ng-form>
</div>

To access nth form in your controller. 要在控制器中访问第n个表单。

  $scope.isValidForm = function(suffix) {
    alert('form '+suffix+' valid='+$scope['form' +suffix].$invalid)
  };

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

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