【发布时间】:2017-01-04 02:48:36
【问题描述】:
我每天都遇到这个问题。当我尝试单击更新图像按钮时会发生以下情况,尽管我的 console.log 它返回了正确的值,但我在第一次尝试时无法进行图像更新。发生的情况是,当我第一次加载更新按钮时向后端发出请求时,它总是返回先前图像的 blob,当我再次尝试时,它返回我尝试在前一刻并插入数据库。简而言之,他总是提出迟到的要求,而我一点也不做,因为。我已经尝试添加 $ 范围。 $apply() 没用。
First update the blob of the previous image
Second update the blob correct of the image
简而言之,我需要更新两次才能将图像更改为我想要的图像。
代码:
function uploadFile(){
var file = $scope.profilePic;
var blob = new Blob([file], {
"type": "text/html"
});
var reader = new FileReader();
reader.onload = function(e) {
$scope.text = e.target.result.split(',')[1];
$scope.loadedUser.profilePic = $scope.text;
}
reader.readAsDataURL(blob);
};
HTML:
<div layout-gt-sm="row" style="margin-bottom: 20px;">
<input type="file" name="profilePic" fileread="profilePic">
</div>
APP:
app.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
scope.$apply(function () {
scope.fileread = changeEvent.target.files[0];
// or all selected files:
// scope.fileread = changeEvent.target.files;
});
});
}
}
}]);
【问题讨论】:
标签: javascript angularjs image angularjs-directive angularjs-scope