AngularJS表格验证和提交

时间:2017-05-10 14:09:21

标签: angularjs

Codepen链接:http://codepen.io/anon/pen/mVyPaw

需要创建一个角度应用程序(w / 1控制器),以确认此表单已填写所有三个字段&只有表单有效时才能启用“提交”按钮。不需要使用预处理器。

<div class="container" ng-app="myApp" ng-controller="formCtrl">

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="form" id="contact-form">
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName">
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName">
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }">
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>

    <button type="submit" class="btn btn-default" ng-model="submitButton">Submit</button>

  </form>

</div>

2 个答案:

答案 0 :(得分:0)

添加所需的验证

 <div class="container" ng-app="myApp" ng-controller="formCtrl">

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="myForm" id="contact-form">
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName" required>
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName" required >
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }" required>
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>

    <button type="submit" class="btn btn-default" ng-model="submitButton" data-ng-disabled="myForm.$invalid" >Submit</button>

  </form>

</div>

以下是您编辑过的CodePen

答案 1 :(得分:0)

实际上它的模块注入问题。 ng-app="myApp" ng-controller="formCtrl"您的myApp模块代码在哪里。

尝试这样。

<div class="container" ng-app>

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="form" id="contact-form" novalidate>
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName" required>
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName" required>
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }" required>
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>


    <button type="submit" class="btn btn-default" ng-disabled="form.$invalid">Submit</button>

  </form>

</div>