8

即使我的所有输入字段都是空白的,我的表单仍显示为有效。我在输入字段中有必需的关键字。

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.min.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="/components/angular/angular.min.js"></script>
    <script type="text/javascript" src="/js/app.js"></script>
  </head>

  <body>
    <main>
      <div class="container">
        <div class="row">
          <section ng-controller="LogInController as loginCtrl">
            <form name="signUpForm" action="/createuser" method="POST" novalidate>
              <fieldset>
              <div class="field input-field">
                Email <br />
                <input type="email" name="email" required />
              </div>

              <div class="field input-field">
                Password <br />
                <input type="password" name="password" required />
              </div>

              <div class="field input-field">
                Confirm Password <br />
                <input type="password" name="confirmation" required />
              </div>

              !! -- Form valid? {{signUpForm.$valid}} -- !!

              <div class="actions">
                <input type="submit" value="Sign Up" />
              </div>
              </fieldset>
            </form>
          </section>
        </div>
      </div>
    </main>
  </body>
</html>

在浏览器中加载它会导致 !! -- 表格有效吗?真的 - !!我认为 Angular 知道所需的标签会使空白字段无效?

4

1 回答 1

9

您应该在每个字段上放置ng-model以在每个字段上启用表单,除非您添加ng-model&name值,否则您的字段将永远不会被视为表单的一部分。并且做改变一件事创建一个父变量,例如form&在其中添加所有范围变量。就像在控制器中做$scope.form = {};& 然后在 UI 上添加所有ng-models 一样form.emailform.password& form.confirmation

为了获得更好的表单验证,请从将调用控制器方法之一的表单和使用指令中删除action和属性。& 通过检查表单是否从该控制器方法调用 post 。methodng-submit$valid

标记

<form name="signUpForm" ng-submit="submit(signUpForm, form)" novalidate>
    <fieldset>
        <div class="field input-field">
            Email
            <br />
            <input type="email" name="email" ng-model="form.email" required />
        </div>

        <div class="field input-field">
            Password
            <br />
            <input type="password" ng-model="form.password" name="password" required />
        </div>

        <div class="field input-field">
            Confirm Password
            <br />
            <input type="password" ng-model="form.confirmation" name="confirmation" required />
        </div>

        !! -- Form valid? {{signUpForm.$valid}} -- !!

        <div class="actions">
            <input type="submit" value="Sign Up" />
        </div>
    </fieldset>
</form>

控制器

$scope.submit = function(form, formData) {
    if (form.$valid) { //submit form if it is valid
        $http.post('/url', {
          data: formData
        })
        .success(function() {
          //code here
        })
        .error(function() {
        })
    } else {
        alert("Please fill all required fields")
    }     
}

希望这已经清除了您对形式的小概念,谢谢。

于 2015-04-09T16:35:54.490 回答