我对 AngularJS 很陌生。假设我的应用程序有一个表单。使用检查器,我注意到如果 angularjs 认为表单无效,它会向表单添加一个 ng-invalid 类。可爱。
所以看来,为了检查表单是否有效,我需要用 Jquery 选择器污染我的代码?!不使用表单 Controller 来指示表单有效性的 angularjs 方法是什么?
最佳答案
当你输入<form>
时在你的 ngApp 中,AngularJS 会自动添加表单 Controller (实际上有一个名为 form
的指令,它添加了必要的行为)。 name 属性的值将绑定(bind)在您的范围内;所以类似 <form name="yourformname">...</form>
将满足:
A form is an instance of FormController. The form instance can optionally be published into the scope using the name attribute.
因此,要检查表单有效性,您可以检查 $scope.yourformname.$valid
的值范围的属性。
更多信息您可以访问 Developer's Guide section关于表单。
关于angularjs - 如何使用 angularjs 检查表单有效性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14299834/