在ajax请求之前使用angularjs验证表单

时间:2015-11-19 06:31:20

标签: javascript jquery angularjs ajax

我有一个像这样的简单形式。我使用angularjs通过ajax提交此表单。我想在发送ajax请求之前验证此表单,并且需要向用户显示错误。我是棱角分明的新手。请帮帮我

这是我的代码。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
<div ng-app="Myapp">
    <div ng-controller="orderFormController">

        Item :   <input type="text" name="item"  ng-model='item' required> <p></p>
     Rate:   <input type="text" name="rate"  ng-model='rate' required> <p></p>

        <button type="button"  ng-click='saveorder()' >SAVE ORDER</button>
    </div>    
    <script>
        var Myapp = angular.module('Myapp', ["ngRoute"]);
        Myapp.controller('orderFormController', ['$scope', '$http', function ($scope, $http) {


            $scope.saveorder = function () {

                 // Validate form here and set the error in form

                 $http.post("order/save/", data).success(function (res, status, headers, config) {
                     $scope.message = res;

                }).error(function (data, status) { 
                    $scope.message = res;
                });
            }

        }]);
    </script>             

3 个答案:

答案 0 :(得分:1)

非常简单的实施可以通过几个步骤完成:

  1. <form>标记与name属性一起使用。使用saveorder ng-submit属性。
  2. 创建错误容器,然后制作容器 可见性取决于错误:<span class="error" ng-show="form.item.$error.required“&gt;
  3. 最终结果应如下所示:

    <div ng-controller="orderFormController">    
        <form name="form" ng-submit="saveorder()">
            Item :   <input type="text" name="item"  ng-model='item' required> <p></p>
            <span class="error" ng-show="form.item.$error.required"> Item field is required!</span>
            Rate:   <input type="text" name="rate"  ng-model='rate' required> <p></p>    
            <span class="error" ng-show="form.rate.$error.required"> Rate field is required!</span>
            <button type="button">SAVE ORDER</button>
        </form>    
    </div>  
    

    您可以阅读有关表单验证的Angular native documentation。那里提供了更多细节。

答案 1 :(得分:0)

阅读本文.. https://scotch.io/tutorials/angularjs-form-validation

它解释得非常好,如果仍然存在混淆,请在做出一些努力之后询问这些。

答案 2 :(得分:0)

您应该使用角度form validation或使用

进行自定义验证

   var Myapp = angular.module('Myapp', ["ngRoute"]);
   Myapp.controller('orderFormController', ['$scope', '$http', function($scope, $http) {
   $scope.formdata = {};

   $scope.saveorder = function(formdata) {
       $scope.error = '';

       // Validate form here and set the error in form
       if (angular.isUndefined(formdata.item) == true || angular.isUndefined(formdata.rate) == true) {
           $scope.error = "Both Item and Rate are required."
           return false;
       } else {
           $scope.error = '';
       }
       $http.post("order/save/", formdata).success(function(res, status, headers, config) {
           $scope.message = res;

       }).error(function(status, res) {
           $scope.message = res;
       });
   }

   }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
<div ng-app="Myapp">
    <div ng-controller="orderFormController">
<span style="color:red">{{error}}</span><br>
        
        Item :   <input type="text" name="item"  ng-model='formdata.item' required> <p></p>
     Rate:   <input type="text" name="rate"  ng-model='formdata.rate' required> <p></p>

        <button type="button"  ng-click='saveorder(formdata)' >SAVE ORDER</button>
    </div>