AngularJS的ng-minlength/ng-maxlength在textarea显示错误消息,但表单正在提交提交


    <form name="reviewForm" ng-submit="Submit();" novalidation>
    <div>
        <div class="label label-primary first">Your Review:</div>
        <div class="form-group has-error second">
            <textarea id="Description" name="Description" ng-minlength=50 ng-maxlength=200 ng-model="Description" ng-required="true" style="width: 500px; height: 200px;"></textarea>
             <label class="control-label" ng-show="submitted && reviewForm.Description.$error.required">Description is required.</label>
            <label class="control-label" ng-show="submitted && reviewForm.Description.$error.minlength">Description is should not be less than 50 charecters.</label>
            <label class="control-label" ng-show="submitted && reviewForm.Description.$error.maxlength">Description is should not be more than 200 charecters.</label>
        </div>
          <div style="clear:both;"></div>
    </div>
    <div>
        <div>
            <button type="submit" class="btn btn-primary" name="btnSubmit" ng-disabled="reviewForm.$invalid" ng-click="submitted=true">Submit</button>
        </div>
    </div>
    </form>
 $scope.Submit = function () {
        var product = {
            Title: $scope.Title,
            description: $scope.Description,
            name: $scope.Name,
            Rating: 3
        }
        var res = $http({
            url: "http://localhost:44826/api/review",
            method: "POST",
            data: $.param(product),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        });
        res.success(
            function (data, status, headers, config) {
                //console.error('Repos error', status, data);
                $scope.isVisible = false;
                $scope.FeedMsg = true;
                $scope.SuccessMsg = "Feedback has been submitted Sucessfully...";
            });
        res.error(function (data, status, headers, config) {
            alert('error');
        });
    }

当我在文本区输入文本低于50或超过200个字符,它显示错误消息,但表单是提交,而点击提交按钮。如果我没有输入任何表单没有提交到服务器显示错误信息。

我希望表单不提交时,点击提交错误信息显示…

试试下面的代码:

HTML:

将表单名称作为Submit()函数的参数传递。

<form name="reviewForm" ng-submit="Submit(reviewForm);" novalidation>
    ...
</form>

JS:

如果表单有效,添加一个条件。

$scope.Submit = function(reviewForm) {
    if (reviewForm.$valid) {
        var product = {
            Title: $scope.Title,
            description: $scope.Description,
            name: $scope.Name,
            Rating: 3
        };
        var res = $http({
            url: "http://localhost:44826/api/review",
            method: "POST",
            data: $.param(product),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        });
        res.success(
                function(data, status, headers, config) {
                    //console.error('Repos error', status, data);
                    $scope.isVisible = false;
                    $scope.FeedMsg = true;
                    $scope.SuccessMsg = "Feedback has been submitted Sucessfully...";
                });
        res.error(function(data, status, headers, config) {
            alert('error');
        });
    }
};

最新更新