1

when i try to upload image from a file type input with angular validation its shows error and won't passes

<input type="file" name="displaypic" ng-model="displaypic" ng-pattern="/\.(jpe?g|png|gif|bmp)$/i" ng-required="true" />
<span ng-show="SignupForm.displaypic.$error.pattern">* Must be an Image</span>  

with css as input.ng-invalid.ng-touched{border: 2px solid red;}

its does not show the error of $error.pattern but just get the css for invalid file and didn't let form to submit

Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
Anis
  • 1,190
  • 2
  • 13
  • 26

1 Answers1

0

You have to use directive for this Create an object in controller

$scope.file={};
$scope.file.fileDetail={};

<input type="file" fileUpload filetoread="file.fileDetail" />

.directive("fileUpload ", [function () {
return {
    scope: {
        filetoread: "="
    },
    link: function (scope, element, attributes) {
        element.bind("change", function (changeEvent) {
            var reader = new FileReader();
            reader.onload = function (loadEvent) {
                scope.$apply(function () {
                    scope.filetoread= loadEvent.target.result;
                });
            }
            reader.readAsDataURL(changeEvent.target.files[0]);
        });
    }
}
 }]);
Vikash Kumar
  • 1,712
  • 1
  • 11
  • 17