$ valid方法无法在Angular中使用

时间:2015-09-21 14:39:19

标签: angularjs

我无法在表单上获取$ valid方法。我需要让这个用于表单验证,不知道我哪里出错了。下面是我正在尝试将输出写入控制台的角度代码。

<body ng-app="submitExample">
  <script>
  angular.module('submitExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.list = [];
      $scope.text = 'hello';
      $scope.submit = function() {
        console.log('is email valid? ' + myform.$valid)
        if ($scope.text) {
          $scope.list.push(this.text);
          $scope.text = '';
        }
      };
    }]);
</script>
<form name="myForm" novalidate ng-submit="submit()" ng-controller="ExampleController">
  Enter text and hit enter:
  <input type="text" ng-model="text" name="text" />
  <label>Email:</label>
  <input type="email" ng-model="email"/>
  <input type="submit" id="submit" value="Submit" />
  <pre>list={{list}}</pre>
</form>
</body>

1 个答案:

答案 0 :(得分:2)

试试这个

HTML

ng-submit="submit(myForm)"

<强> JS

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