So I'm creating directive that will match passwords. I was trying to figure out my own way to do it. And when, finally, I though I had found solution it didn't work. Here is the dricetive code:
directive('matchPass', function () {
return {
//restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
scope.$watch(attrs.ngModel, function (n) {
ngModelCtrl.$setValidity('matchPass', n == scope.registerForm.password.$viewValue)
})
scope.$watch(attrs.matchPass, function (n) {
ngModelCtrl.$setValidity('matchPass', n == scope.registerForm.password.$viewValue)
})
}
}
})
The problem is here
scope.$watch(attrs.matchPass...
It doesn't fire, the first one works fine. I'm stuck and lost.
View code
<div class="marginAuto">
<md-input-container>
<label>Password</label>
<input required="" name="password" ng-model="password" type="password" ng-minlength="8" ng-maxlength="30">
<div ng-messages="registerForm.password.$error">
<div ng-message="required">This is required!</div>
<div ng-message-exp="['required', 'minlength', 'maxlength']">
8 - 30 characters!
</div>
</div>
</md-input-container>
</div>
<div class="marginAuto">
<md-input-container>
<label>Repeat password</label>
<input required name="repassword" ng-model="repassword" type="password" match-pass="password">
<div ng-messages="registerForm.repassword.$error" na>
<div ng-message="required">This is required!</div>
</div>
</md-input-container>
</div>
<div class='msg-block' ng-show='registerForm.$error'>
<span class='msg-error' ng-show='registerForm.repassword.$error.matchPass'>
Passwords don't match.
</span>
</div>