如何在控制器

时间:2015-12-16 13:36:56

标签: angularjs angularjs-scope angularjs-controller angularjs-forms

如何在控制器中验证我的角度形式?我需要检查表格并依赖于有效性显示文本块

我使用了官方angularjs网站上的一个表格示例。

有人可以解释为什么这段代码console.log($scope.myForm);输出未定义,我该如何获得表单?



angular.module('formExample', [])
    .controller('FormController', ['$scope', function($scope) {
      $scope.userType = 'guest';
      
      if(myForm.$valid){ //I tried to validate form
        console.log('Form is valid   ' + $scope.myForm.$valid);
      }else{
        console.log('form is not valid ' + $scope.myForm.$valid);
      }
    }]);

 .my-form {
   transition:all linear 0.5s;
   background: transparent;
 }
 .my-form.ng-invalid {
   background: red;
 }
</s
&#13;
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example59-production</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular-animate.js"></script>
  

  <script type="text/javascript">
    angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
  </script>
</head>
<body ng-app="formExample">
<form name="myForm" ng-controller="FormController" class="my-form">
  userType: <input name="input" ng-model="userType" required>
  <span class="error" ng-show="myForm.input.$error.required">Required!</span><br>
  <code>userType = {{userType}}</code><br>
  <code>myForm.input.$valid = {{myForm.input.$valid}}</code><br>
  <code>myForm.input.$error = {{myForm.input.$error}}</code><br>
  <code>myForm.$valid = {{myForm.$valid}}</code><br>
  <code>myForm.$error.required = {{!!myForm.$error.required}}</code><br>
 </form>
</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案