首页 > 解决方案 > 验证表单 ng-click 功能不起作用后加载页面

问题描述

我是 angularjs 的新手。我正在验证触发保存功能的表单。

我将控件放在表单中并将其命名为myform

ng-required="true"在名称和密码文本框中使用。

保存按钮中,我调用了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>

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>

现在它将触发验证消息

验证消息截图

输入姓名和密码并提交表单后,页面加载,但没有触发警报消息。

标签: javascriptangularjs

解决方案


我将假设它正在尝试调用表单的操作,这没什么,所以它会重新加载页面。您可以使用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>

或者,如果您真的想保留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>


推荐阅读