简体   繁体   English

Angular JS和验证逐步

[英]Angular JS and validation step by step

I have an angular js wizard with steps: 我有一个带有步骤的js向导:

<wizard on-before-step-change="log(event)" on-step-changing="log(event)" on-after-step-change="log(event)" user="user">

<step title="step 1">

</step>

<step title="step 2">

</step>

<step title="step 3">

</step>

</wizard>

and in each of these steps there are input fields and I am looking to validate each step at a time when they click my next button: 并且在每个步骤中都有输入字段,当他们单击我的下一个按钮时,我希望一次验证每个步骤:

<a class="btn btn-default" ng-click="gotoNextStep()" ng-show="showNextButton()">Next</a>

and here is the click event: 这是点击事件:

$scope.gotoNextStep = function () {
          //Validation
          toggleSteps($scope.currentStepIndex + 1);
      }

I was looking to use jQuery validation and have the required class in each of my input fields, but if I went down that road how would I have it validate one step at a time? 我一直在使用jQuery验证,并且在每个输入字段中都有必需的类,但是如果我走那条路,我如何一次验证一个步骤呢? Or is there a better way to go about this? 还是有更好的方法来解决这个问题? I have did some searching on the subject, but I found the examples I found very confusing and complicated...any help would be much appreciated. 我已经对该主题进行了一些搜索,但是我发现我发现的示例非常令人困惑和复杂……任何帮助将不胜感激。

THanks. 谢谢。

Angular provides a framework for validating forms ( documentation ) and you can verify based upon $valid (or $invalid ) for each step. Angular提供了一个用于验证表单( 文档 )的框架,您可以根据每个步骤的$valid (或$invalid )进行验证。

Also note that you can have one parent form, with each step utilizing the ng-form directive to work as a nested form. 还要注意,您可以有一个父表单,每个步骤都使用ng-form指令作为嵌套表单。

I would add the following: 我将添加以下内容:

<wizard>
  <step> <!-- step 1 -->
    <div ng-form="step1" name="step1">
      <button ng-disabled="step1.$invalid">Next Step</button>
    </div>
  </step>
</wizard>

If you need to define a new validation, you can follow this excellent article 如果您需要定义新的验证,则可以阅读这篇出色的文章

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

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