Skip to content

Instantly share code, notes, and snippets.

@gruppjo
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gruppjo/fc54ff4ede0f2db2ba7c to your computer and use it in GitHub Desktop.
Save gruppjo/fc54ff4ede0f2db2ba7c to your computer and use it in GitHub Desktop.
match directive
'use strict';
angular.module('crmApp')
.directive('match', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
match: '='
},
link: function (scope, element, attrs, ngModel) {
scope.modelValue = '';
scope.matchValue = '';
scope.validate = function () {
var isValid = scope.modelValue === scope.matchValue;
ngModel.$setValidity('match', isValid); // TODO: assigns undefined to the model. why?
return isValid;
};
// watch for changes in ngModel
scope.$watch(function () {return ngModel.$viewValue;}, function (value) {
scope.modelValue = value;
scope.validate();
});
// watch for changes in match
scope.$watch('match', function (value) {
scope.matchValue = value || ''; // set to empty string if undefined
scope.validate();
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment