如何使用 angularjs 检查表单有效性? [英] How do I check form validity with angularjs?

查看:29
本文介绍了如何使用 angularjs 检查表单有效性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 angularjs 很陌生.假设我的应用程序有一个表单.使用检查器,我注意到如果 angularjs 认为表单无效,它会向表单添加一个 ng-invalid 类.可爱.

I'm very new to angularjs. Say my app has a form. Using the inspector, I noticed that if angularjs thinks that the form is invalid, it adds an ng-invalid class to the form. Lovely.

所以似乎为了检查表单是否有效,我需要用 Jquery 选择器污染我的代码?!angularjs 在不使用表单控制器的情况下指示表单有效性的方法是什么?

So it seems that in order to check if the form is valid I need to pollute my code with Jquery selector?! What is the angularjs way to indicate form validity without using a form controller?

推荐答案

当你把

标签放在你的 ngApp 中时,AngularJS 会自动添加表单控制器(实际上有一个指令,叫做 form 添加必要的行为).name 属性的值将绑定在您的范围内;所以像 <form name="yourformname">...</form> 会满足:

When you put <form> tag inside you ngApp, AngularJS automatically adds form controller (actually there is a directive, called form that add nessesary behaviour). The value of the name attribute will be bound in your scope; so something like <form name="yourformname">...</form> will satisfy:

表单是 FormController 的一个实例.可以选择使用 name 属性将表单实例发布到作用域中.

A form is an instance of FormController. The form instance can optionally be published into the scope using the name attribute.

因此要检查表单有效性,您可以检查范围的 $scope.yourformname.$valid 属性的值.

So to check form validity, you can check value of $scope.yourformname.$valid property of scope.

您可以在开发人员指南部分中获取有关表单的更多信息.

More information you can get at Developer's Guide section about forms.

这篇关于如何使用 angularjs 检查表单有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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