验证不适用于具有“必需"属性的文件输入 - AngularJS [英] Validation doesnt work for File Input with 'Required' attribute- AngularJS
问题描述
我一直在玩这个,但无法让它工作.我正在创建一个有角度的表单,当 required
属性添加到文本字段时,我能够使验证工作.但是,如果输入类型 file
添加了 required
属性,我注意到显示了 $error.required
文本,但它甚至没有验证如果选择了一个文件.即使添加了文件,它仍然显示为无效.我在 jsfiddle 中创建了一个示例,因此您可以查看:http://jsfiddle.net/Alien_time/kxSaz/6/
I have been playing around this and couldnt get it to work. I was creating an angular form and I was able to get the validation to work when required
attribute is added to the text field. However, if a input type file
is added with required
attribution, I noticed that the $error.required
text is shown but it doesnt validation even if a file is chosen. Its still showing as invalid even after adding a file. I have created a sample in jsfiddle so you can check this out: http://jsfiddle.net/Alien_time/kxSaz/6/
文件输入的验证不起作用吗?使用文件选择时如何添加必需选项并验证它?
推荐答案
ngModelController 没有t 目前支持输入类型=文件.
ngModelController doesn't currently support input type=file.
您可以使用自定义指令解决您的问题.
you can solve your issue with a custom directive.
app.directive('validFile',function(){
return {
require:'ngModel',
link:function(scope,el,attrs,ngModel){
el.bind('change',function(){
scope.$apply(function(){
ngModel.$setViewValue(el.val());
ngModel.$render();
});
});
}
}
});
这篇关于验证不适用于具有“必需"属性的文件输入 - AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!