我在离子应用程序(the code available here)中有注册表单,注册视图有自己的控制器。密码确认有一个指令来检查匹配主密码。注册按钮被禁用,直到表格有效。但表单永远不会有效。 password_c
输入始终无效。
<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
答案 0 :(得分:1)
我现在无法测试,但我相信$parsers
希望您返回一个值,除非您有解析器错误。
尝试在代码中添加return
:
ctrl.$parsers.unshift(function (viewValue) {
var noMatch = viewValue != scope.regForm.password.$viewValue;
ctrl.$setValidity('noMatch', !noMatch);
return viewValue;
});