原生的ng並不支援file element的validation,可透過自已新增directive來達到這個功能。
可以參考這篇StackoverFlow: http://stackoverflow.com/questions/16207202/required-attribute-not-working-with-file-input-in-angular-js
directive
app.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(); }); }); } } });
HTML
Input is valid: {{myForm.userUpload.$valid}} Selected file: {{filename}}
沒有留言:
張貼留言
留個話吧:)