Required attribute Not working with File input in Angular Js

前端 未结 2 504
庸人自扰
庸人自扰 2020-11-30 05:03

I have a file upload control in my form.I am using Angular JS . When I put the required attribute to validate that the file is selected it is not working.

&l         


        
2条回答
  •  生来不讨喜
    2020-11-30 05:34

    Extending @joakimbl code I will suggest the direct like this

    .directive('validFile',function(){
        return {
            require:'ngModel',
            link:function(scope,el,attrs,ctrl){
                ctrl.$setValidity('validFile', el.val() != '');
                //change event is fired when file is selected
                el.bind('change',function(){
                    ctrl.$setValidity('validFile', el.val() != '');
                    scope.$apply(function(){
                        ctrl.$setViewValue(el.val());
                        ctrl.$render();
                    });
                });
            }
        }
    })
    

    and in html you can use like this

    
    
    

提交回复
热议问题