HTML:
<form name="form">
<input type="file" ng-model="document" valid-file required>
<input type="submit" value="{{ !form.$valid && 'invalid' || 'valid' }}">
</form>
用于侦听 input[type=file] 更改的自定义指令:
myApp.directive('validFile',function(){
return {
require:'ngModel',
link:function(scope,el,attrs,ngModel){
//change event is fired when file is selected
el.bind('change',function(){
scope.$apply(function(){
ngModel.$setViewValue(el.val());
ngModel.$render();
});
});
}
};
});
选择文件时,控制台中会出现以下错误:
错误:InvalidStateError:DOM 异常 11 错误:尝试使用不可用或不再可用的对象。
尝试使用 plunkr:http ://plnkr.co/edit/C5j5e0JyMjt9vUopLDHc?p=preview
如果没有该指令,输入文件字段的状态将不会被推送到 form.$valid。任何想法为什么我会收到此错误以及如何解决此错误?