验证 – AngularJS:有什么方法来确定哪些字段使表单无效?
发布时间:2020-12-17 09:07:03 所属栏目:安全 来源:网络整理
导读:我有一个AngularJS应用程序中的以下代码,在控制器内部, 它从ng-submit函数调用,它属于名为profileForm的表单: $scope.updateProfile = function() { if($scope.profileForm.$invalid) { //error handling.. } //etc.}; 在这个函数内部,有没有办法弄清楚
我有一个AngularJS应用程序中的以下代码,在控制器内部,
它从ng-submit函数调用,它属于名为profileForm的表单: $scope.updateProfile = function() { if($scope.profileForm.$invalid) { //error handling.. } //etc. }; 在这个函数内部,有没有办法弄清楚哪些字段导致整个被称为无效?
每个输入名称的验证信息作为属性在窗体的名称范围内公开。
HTML <form name="someForm" action="/"> <input name="username" required /> <input name="password" type="password" required /> </form> JS $scope.someForm.username.$valid // > false $scope.someForm.password.$error // > { required: true } 暴露的属性是$ pristine,$ dirty,$ valid,$ invalid,$ error。 如果你想迭代的错误有一些原因: $scope.someForm.$error // > { required: [{$name: "username",$error: true /*...*/},// {$name: "password",/*..*/}] } 每个出错的规则将暴露在$ error中。 这里是一个plunkr玩http://plnkr.co/edit/zCircDauLfeMcMUSnYaO?p=preview (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |