我在表单中有一个文件类型的输入元素。我想创建一个自定义指令,在使用输入元素选择文件后立即检查文件大小。我知道如何创建自定义指令,但在AngularJS中是否有一种方法可以确定所选元素的文件大小。不要使用JQuery。
js代码:
app.directive('checkFileSize',function(){
return{
require: 'ngModel',
link: function(scope, elem, attr, ctrl) {
// add a parser that will process each time the value is
// parsed into the model when the user updates it.
ctrl.$parsers.unshift(function (value) {
//i want to do something like this
var fileSize= // get file size here
if(fileSize>threshold){
ctrl.$setValidity('checkFileSize',false);
}
// return the value to the model,
return someValue;
});
}
}
});