How Dan correctly asks in his comment on the accepted answer
- ? ?
, ctrl .
require: 'ngModel'
ng-controller, .
, , , .
myApp.directive('maxFileSize', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
var maxSize = Number(attrs.maxFileSize);
var sizeHandler = function () {
if (element.get(0).files.length) {
var fileSize = element.get(0).files[0].size;
if (fileSize > maxSize) {
ctrl.$setValidity("fileSize", false);
} else {
ctrl.$setValidity("fileSize", true);
}
}
};
element.bind('change', sizeHandler);
}
};
});