【问题标题】:Input type="file" show error when i upload a image file上传图片文件时输入类型=“文件”显示错误
【发布时间】:2018-01-27 02:04:57
【问题描述】:

当我尝试从带有角度验证的文件类型输入上传图像时,它显示错误并且不会通过

<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>  

使用 css 作为 input.ng-invalid.ng-touched{border: 2px solid red;}

它没有显示$error.pattern的错误,只是获取无效文件的css并且没有让表单提交

【问题讨论】:

标签: javascript html css angularjs validation


【解决方案1】:

您必须为此使用指令 在控制器中创建一个对象

$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]);
        });
    }
}
 }]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 2014-10-23
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多