AngularJS检查表单在控制器中是否有效

时间:2013-11-18 17:32:59

标签: angularjs

我需要检查一个表单在控制器中是否有效。

查看:

<form novalidate=""
      name="createBusinessForm"
      ng-submit="setBusinessInformation()"
      class="css-form">
 <!-- fields -->
</form>

在我的控制器中:

.controller(
    'BusinessCtrl',
    function ($scope, $http, $location, Business, BusinessService, 
              UserService, Photo)
    {

        if ($scope.createBusinessForm.$valid) {
            $scope.informationStatus = true;
        }

        ...

我收到了这个错误:

TypeError: Cannot read property '$valid' of undefined

4 个答案:

答案 0 :(得分:106)

试试这个

在视图中:

<form name="formName" ng-submit="submitForm(formName)">
 <!-- fields -->
</form>
控制器中的

$scope.submitForm = function(form){
  if(form.$valid) {
   // Code here if valid
  }
};

在视图中:

<form name="formName" ng-submit="submitForm(formName.$valid)">
  <!-- fields -->
</form>
控制器中的

$scope.submitForm = function(formValid){
  if(formValid) {
    // Code here if valid
  }
};

答案 1 :(得分:29)

我已将控制器更新为:

.controller('BusinessCtrl',
    function ($scope, $http, $location, Business, BusinessService, UserService, Photo) {
        $scope.$watch('createBusinessForm.$valid', function(newVal) {
            //$scope.valid = newVal;
            $scope.informationStatus = true;
        });
        ...

答案 2 :(得分:14)

这是另一种解决方案

在html中设置隐藏的范围变量,然后您可以从控制器中使用它:

<span style="display:none" >{{ formValid = myForm.$valid}}</span>

以下是完整的工作示例:

&#13;
&#13;
angular.module('App', [])
.controller('myController', function($scope) {
  $scope.userType = 'guest';
  $scope.formValid = false;
  console.info('Ctrl init, no form.');
  
  $scope.$watch('myForm', function() {
    console.info('myForm watch');
    console.log($scope.formValid);
  });
  
  $scope.isFormValid = function() {
    //test the new scope variable
    console.log('form valid?: ', $scope.formValid);
  };
});
&#13;
<!doctype html>
<html ng-app="App">
<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
</head>
<body>

<form name="myForm" ng-controller="myController">
  userType: <input name="input" ng-model="userType" required>
  <span class="error" ng-show="myForm.input.$error.required">Required!</span><br>
  <tt>userType = {{userType}}</tt><br>
  <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br>
  <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br>
  <tt>myForm.$valid = {{myForm.$valid}}</tt><br>
  <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br>
  
  
  /*-- Hidden Variable formValid to use in your controller --*/
  <span style="display:none" >{{ formValid = myForm.$valid}}</span>
  
  
  <br/>
  <button ng-click="isFormValid()">Check Valid</button>
 </form>
</body>
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:4)

BusinessCtrlcreateBusinessForm FormController之前初始化。 即使您在表单上有ngController也不会按照您想要的方式运行。 你无法帮助(你可以创建你的ngControllerDirective,并尝试欺骗优先级。)这就是angularjs的工作方式。

请参阅此plnkr示例: http://plnkr.co/edit/WYyu3raWQHkJ7XQzpDtY?p=preview