Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Created April 18, 2016 06:59
Show Gist options
  • Save bhavyaw/024cffa70acfcc5d7474a94e48b5ac20 to your computer and use it in GitHub Desktop.
Save bhavyaw/024cffa70acfcc5d7474a94e48b5ac20 to your computer and use it in GitHub Desktop.
ngValueEqualsDirective ( used in confirm password )
(function(){
'use strict';
angular
.module('app')
.directive('equals',valueEquals);
function valueEquals(){
return {
require: 'ngModel',
link: function (scope, elem, attrs, model) {
if (!attrs.equals) {
console.error('equals expects a model as an argument!');
return;
}
scope.$watch(attrs.equals, function (value) {
// Only compare values if the second ctrl has a value.
if (model.$viewValue !== undefined && model.$viewValue !== '') {
model.$setValidity('equals', value === model.$viewValue);
}
});
model.$parsers.push(function (value) {
// Mute the nxEqual error if the second ctrl is empty.
if (value === undefined || value === '') {
model.$setValidity('equals', true);
return value;
}
var isValid = value === scope.$eval(attrs.equals);
model.$setValidity('equals', isValid);
return isValid ? value : undefined;
});
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment