控制器中的angular.js表单验证 [英] angular.js form validation in controller

查看:99
本文介绍了控制器中的angular.js表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下表格:

<form ng-controller="SomeCtrl as some" name="product[[product.id]]" ng-submit="some.addToCart(something, product[[product.id]].$valid)" novalidate></form>

然后html如下:

<form ng-controller="SomeCtrl as some" name="product1" ng-submit="some.addToCart(something, product1.$valid)" novalidate=""></form>

控制器:

this.addToCart = function(something, isValid) {
    console.log(isValid);
}

isValid始终为 undefined .如何在控制器中检测表格是否有效?

isValid is always undefined. How to detect if form is valid in controller?

推荐答案

演示如果您想在控制器中进行自己的验证,则不想使用ng-submit,因为如果验证无效,它将阻止表单的提交.

Demo You don't want to use ng-submit if you want to do your own validation in the controller because it will block the submission of the form if it is invalid.

只需使用带有ng-click功能的常规按钮即可检查表单的状态.

Just use a regular button with a function in ng-click that checks the condition of the form.

控制器:

  $scope.submit = function() {
    console.log($scope.myForm.$valid)
  }

HTML

<form name="myForm">
  <input ng-model="myForm.text" type="text" required />
  <button ng-click="submit()">Submit</button>
</form>

这篇关于控制器中的angular.js表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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