我正在尝试在我的应用程序中设置自定义验证,它适用于一次场景。当我做焦点或标签时,它工作正常。但是,我正在处理另一个场景,在从控制器调用函数的同时,我正在广播一个事件并在指令中处理它。请找到代码段: -
HTML代码段
<md-input-container class="md-block" md-no-float>
<input type='text' id='signInUsername' ng-model="formData.email" signinauthvalidator name="username" required placeholder="Username ( eg:- bill2016 )">
<div ng-messages="signInForm.username.$error" class='error-messages'>
<div ng-message="required">This is required</div>
<div ng-message="countValidator">Minimum of 4 characters and maximum of 64 is allowed.</div>
<div ng-message="userExist">User doesn't exist.</div>
</div>
</md-input-container>
<md-input-container class="md-block" md-no-float>
<input id='password' signinauthvalidator type="{{si.showpw? 'text': 'password'}}" placeholder="Password" required name="password" ng-model="formData.password">
<div ng-messages="signInForm.password.$error" class='error-messages'>
<div ng-message="required">This is required.</div>
<div ng-message="passwordCountValidator">Password should be 8-30 characters</div>
<div ng-message="passwordInvalid">Invalid password.</div>
</div>
</md-input-container>
指令代码段
scope.$on('authError', function (e,v) {
if(v == 'PASSWORD_INVALID') {
ctrl.$setValidity('passwordInvalid', false);
} else if(v == 'USERNAME_INVALID') {
ctrl.$setValidity('userExist', false);
}
});
ctrl.$parsers.push(customValidator);
正在发生的是,当此代码ctrl.$setValidity('passwordInvalid', false);
执行时,此验证也会被注入到用户名字段中。我检查了表单响应,我可以在用户名和密码字段中看到passwordInvalid
字段。如何才能使其仅适用于密码字段?感谢