是否可以在输入类型="文件"

时间:2017-12-15 12:27:01

标签: javascript jquery angularjs

这是在按钮点击的打印机上打印上传的照片。
当用户没有上传图片时是否可以打印任何虚拟照片?



var app = angular.module('myApp', [])
app.controller('myController', function($scope) {
   
    $scope.saveDetails = function(doc) { 
         console.log(doc.file)   
            
    };
})
app.directive("fileread", [function () {
    return {
        scope: {
            fileread: "="
        },
        link: function (scope, element, attributes) {
            element.bind("change", function (changeEvent) {
                var reader = new FileReader();
                reader.onload = function (loadEvent) {
                    scope.$apply(function () {
                        scope.fileread = loadEvent.target.result;
                    });
                }
                reader.readAsDataURL(changeEvent.target.files[0]);
            });
        }
    }
}]);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController" ng-init="init()">
     <div>upload profile photo</div>
    <input type="file" id="file1" name="file"  fileread="doc.file"/>
    <button ng-click="saveDetails(doc)">save</button>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在$ scope.saveDetails函数内检查doc。如果未定义,请指定默认值。

$scope.saveDetails = function(doc) { 
     if(doc === undefined) {
          // assign a dummy photo.      
     }
     console.log(doc.file)   

};