AngularJs-表单验证指令。密码确认始终无效
问题描述:
我在我的离子应用程序(the code available here)中有注册表单,注册视图有它自己的控制器。密码确认有一个指令来检查与主密码的匹配。注册按钮被禁用,直到表单有效。但该表格永远不会生效。 password_c
输入总是无效。AngularJs-表单验证指令。密码确认始终无效
<ion-view view-title="Register Form">
<ion-content>
<form ng-submit="signup()" name="regForm" novalidate>
<div>
<label for="email" .....></label>
<label for="password" ......></label>
<label for="password_c">
<input type="password" id="password_c" name="password_c" ng-model="registerForm.password_c" valid-password-c required>
<span ng-show="regForm.password_c.$error.required && regForm.password_c.$dirty">Please confirm your password.</span>
<span ng-show="!regForm.password_c.$error.required && regForm.password_c.$error.noMatch && regForm.password.$dirty">Passwords do not match.</span>
</label>
<button type="submit" ng-disabled="!regForm.$valid">
</div>
</form>
</ion-content>
</ion-view>
,这是指令:
.directive('validPasswordC', function() {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var noMatch = viewValue != scope.regForm.password.$viewValue
ctrl.$setValidity('noMatch', !noMatch)
})
}
}
})
我做console.log
的ng-show
条件;当密码匹配时,条件变得不明确。
console.log(!regForm.password_c.$error.required && regForm.password_c.$error.noMatch && regForm.password.$dirty)
另外;首先.required
变得不确定,然后.noMatch
答
我现在不能测试它,但我相信$parsers
期望您除非有解析器错误才会返回一个值。
试着在你的代码中添加return
:
ctrl.$parsers.unshift(function (viewValue) {
var noMatch = viewValue != scope.regForm.password.$viewValue;
ctrl.$setValidity('noMatch', !noMatch);
return viewValue;
});
你救了我的命:)它的工作 –
只知道,为什么在纯AngularJs Apps是需要和工作不归路?像这个例子一样:http://jsfiddle.net/thomporter/UZrex/1/ –