Last active
August 29, 2015 14:03
-
-
Save gruppjo/fc54ff4ede0f2db2ba7c to your computer and use it in GitHub Desktop.
match directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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