JavaScript/Angular 触发器表单验证

标签 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">

最佳答案

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

所以我认为您不能提交表单。不过,您可以点击提交按钮:

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

更新:

如果您想以编程方式更改值以触发 Angular 的摘要循环,即更新表单验证,您必须访问 Angular 范围:

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();
});

关于JavaScript/Angular 触发器表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443995/

相关文章:

javascript - 开 Jest 单元测试: setTimeout not firing in async test

javascript - 使用确认模式删除数据

javascript - 用于将子文档连接到其父文档的端点,不返回任何内容

jquery - 在 iframe 和父级之间传递 jquery 变量

jquery - jquery 移动 header 中的垂直控制组

javascript - Mailchimp 通过 jQuery Ajax。提交正确,但 JSON 响应作为单独的页面返回

html - 输入在 <details> 标签内变得困惑

html - 是否有适合添加到 html 文件中的图像链接?

javascript - HTML 到 JS 数据表 : Still show a removed row after sorting or page size change?

javascript - Meteor:从错误响应中获取数据?