Created
October 31, 2014 11:48
-
-
Save guilbep/55c7b5fa228b139758c1 to your computer and use it in GitHub Desktop.
setValidity
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
module.directive('setValidity', ['$parse', | |
function($parse) { | |
// no need for form. better | |
return { | |
require: "ngModel", | |
scope: { | |
'setValidity': '=setValidity', | |
'ngDisabled': '=ngDisabled' | |
}, | |
controller: function($scope, $element, $attrs, $transclude) { | |
$scope.isEmpty = function(elements) { | |
var type = typeof(elements); | |
var sum = 0; | |
// what | |
switch (type) { | |
case 'object': | |
angular.forEach(elements, function(value, key) { | |
sum += value.length; | |
}); | |
break; | |
case 'number': | |
sum = elements; | |
break; | |
default: | |
// It will never work but eyh (in case undefined) | |
sum = elements || 0; | |
break; | |
} | |
return sum === 0; | |
}; | |
}, | |
link: function(scope, ele, attrs, c) { | |
var getValidity = function() { | |
var disable = scope.ngDisabled; | |
var empty = scope.isEmpty(scope.setValidity); | |
return (disable || !empty); | |
}; | |
c.$setValidity('custom', getValidity()); | |
scope.$watch('setValidity', function(newv) { | |
c.$setValidity('custom', getValidity()); | |
}, true); | |
scope.$watch('ngDisabled', function() { | |
c.$setValidity('custom', getValidity()); | |
}); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment