更新:
如果我用下面的简单代码填写 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/