简体   繁体   English

验证表单ng-click函数无效后加载页面

[英]Page was loaded after validate the form ng-click function not working

I am new to angularjs. 我是angularjs的新手。 I am validating the form on triggering the save function. 我正在验证触发保存功能的表格。

I placed my control inside the form and named it myform . 我将控件放置在表单中,并将其命名为myform

I am using ng-required="true" in Name and Password textboxes. 我在ng-required="true"名称和密码”文本框中使用ng-required="true"

In the ng-click of the Save button, I call the save() function 在“ 保存”按钮的ng-click中,我调用save()函数

<div ng-app="myApp" ng-controller="myControl">
    <form name="myForm" novalidate>
    <table>
        <tr>
            <td>
                Name
            </td>
            <td>
                <input type="text" class="form-control" id="txtName" name="Name" placeholder="Name"
                    ng-model="Name" ng-required="true">
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <input type="password" class="form-control" id="txtPassword" placeholder="Password"
                    ng-required="true" name="Password" ng-model="Password">
            </td>
        </tr>
        <tr>
            <td>
                Email
            </td>
            <td>
                <input type="email" class="form-control" id="Email" placeholder="Email" name="Email"
                    ng-model="Email">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <button ng-click="save()"> Save </button>
            </td>
        </tr>
    </table>
    </form>
  </div>

In the save() function, I validate the form and place an alert saying if the form is valid or invalid. save()函数中,我验证该表单并发出警告,说明该表单有效还是无效。

<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller("myControl", function ($scope, $http) {
        $scope.save = function () {
            if ($scope.myForm.$valid) {
                alert('Valid');
            } else {
                alert('Invalid');
            }
        };
    });
</script>

Now it will trigger validation message 现在它将触发验证消息

验证消息截图

After entering name and password and submitting the form, the page loaded, but didn't trigger an alert message. 输入名称和密码并提交表单后,页面已加载,但未触发警告消息。

I'm going to assume that it's trying to call the form's action, which is nothing so it will reload the page. 我将假设它正在尝试调用表单的操作,这没什么,所以它将重新加载页面。 You can use ng-submit which will prevent the default action. 您可以使用ng-submit来阻止默认操作。

 var app = angular.module("myApp", []); app.controller("myControl", function($scope, $http) { $scope.save = function() { if ($scope.myForm.$valid) { alert('Valid'); } else { alert('Invalid'); } }; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <div ng-app="myApp" ng-controller="myControl"> <form name="myForm" novalidate ng-submit="save()"> <table> <tr> <td> Name </td> <td> <input type="text" class="form-control" id="txtName" name="Name" placeholder="Name" ng-model="Name" ng-required="true"> </td> </tr> <tr> <td> Password </td> <td> <input type="password" class="form-control" id="txtPassword" placeholder="Password" ng-required="true" name="Password" ng-model="Password"> </td> </tr> <tr> <td> Email </td> <td> <input type="email" class="form-control" id="Email" placeholder="Email" name="Email" ng-model="Email"> </td> </tr> <tr> <td colspan="2" align="right"> <button type="submit"> Save </button> </td> </tr> </table> </form> </div> 

Or if you really want to keep ng-click you can pass the event in the save method and prevent it there 或者,如果您确实想保持ng-click ,则可以在save方法中传递事件并在该位置阻止它

 var app = angular.module("myApp", []); app.controller("myControl", function($scope, $http) { $scope.save = function(e) { e.preventDefault() if ($scope.myForm.$valid) { alert('Valid'); } else { alert('Invalid'); } }; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <div ng-app="myApp" ng-controller="myControl"> <form name="myForm" novalidate> <table> <tr> <td> Name </td> <td> <input type="text" class="form-control" id="txtName" name="Name" placeholder="Name" ng-model="Name" ng-required="true"> </td> </tr> <tr> <td> Password </td> <td> <input type="password" class="form-control" id="txtPassword" placeholder="Password" ng-required="true" name="Password" ng-model="Password"> </td> </tr> <tr> <td> Email </td> <td> <input type="email" class="form-control" id="Email" placeholder="Email" name="Email" ng-model="Email"> </td> </tr> <tr> <td colspan="2" align="right"> <button type="submit" ng-click="save($event)"> Save </button> </td> </tr> </table> </form> </div> 

Here is what you should do 这是你应该做的

<form ng-submit="myForm.$valid && save()" name="myForm">
    <button type="submit">Submit</button>
</form>

This simple snippet ng-submit="myForm.$valid && save()" will not call save if form is not valid. 如果表单无效,此简单代码段ng-submit="myForm.$valid && save()"将不会调用save And this is the angular way - keep all the validation apart from business logic. 这是有角度的方法-将所有验证与业务逻辑分开。 save method should be executed only if all the form data is valid. 仅当所有表单数据均有效时,才应执行save方法。

There are a lot of base input validation directives: minlength, maxlength, required, pattern and etc. . 基本输入验证指令很多: minlength, maxlength, required, pattern and etc. AngularJS has a great mechanism that allows you to create your own validation ( Here is a great article of how to implement custom validation for inputs, just in case). AngularJS有一个很棒的机制,允许您创建自己的验证( 是一篇很棒的文章,介绍了如何为输入实现自定义验证,以防万一。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM