JavaScript /角度触发器表单验证

时间:2017-04-17 01:32:56

标签: javascript jquery html angularjs

更新

如果我使用下面的简单代码填写AngularJS表单。

 document.getElementByID("username").value = "ZSAdmin"
 document.getElementByID("password").value = "SuperSecure101"

如何在调用之前生成AngularJS表单验证:

document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click()


<form name="loginForm" ng-submit="login()" autocomplete="off" class="ng-dirty ng-valid ng-valid-required">
<button type="submit" class="icon-login" ng-disabled="!loginForm.$valid"></button>
<input type="text" name="username" id="username" placeholder="Username / Email" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.username" class="ng-dirty ng-valid ng-valid-required">
<input type="password" name="password" id="password" placeholder="Password" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.password" class="ng-dirty ng-valid ng-valid-required">

1 个答案:

答案 0 :(得分:2)

根据ng-submit的文档,它&#34;阻止默认操作(表单意味着将请求发送到服务器并重新加载当前页面),但仅限于表单不包含操作,数据操作或x-action属性。&#34;

所以我认为你不能提交表格。您可以点击提交按钮:

document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click()

更新

如果要以编程方式更改值以触发角度的摘要周期,即更新表单验证,则必须访问角度范围:

var $scope = angular.element("#username").scope();
$scope.$apply(function() {
  $scope.credentials.username = "ZSAdmin";
  $scope.credentials.password = "SuperSecure101";
  #you can even trigger the login from here if you want instead of the button click in the answer above:
  $scope.login();
});