angular.module('app',[])
.controller("upload", ['$scope', '$http', 'uploadService', function($scope, $http, uploadService) {
$scope.$watch('file', function(newfile, oldfile) {
if(angular.equals(newfile, oldfile)){
return;
}
});
}])
.service("uploadService", function($http, $q) {
return ({
upload: upload
});
function upload(file) {
var upl = $http({
method: 'POST',
url: '', // /api/upload
headers: {
'Content-Type': ''
},
data: {
upload: file
},
transformRequest: function(data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function(value, key) {
formData.append(key, value);
});
var headers = headersGetter();
delete headers['Content-Type'];
return formData;
}
});
return upl.then(handleSuccess, handleError);
}
})
.directive("fileinput", [function() {
return {
scope: {
fileinput: "=",
filepreview: "="
},
link: function(scope, element, attributes) {
element.bind("change", function(changeEvent) {
scope.fileinput = changeEvent.target.files[0];
var reader = new FileReader();
reader.onload = function(loadEvent) {
scope.$apply(function() {
scope.filepreview = loadEvent.target.result;
});
}
reader.readAsDataURL(scope.fileinput);
});
}
}
}]);
img.art_img_prev_use
{
width:250px;
\t height:250px;
position: absolute;
top:0px;
left: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 class="prev_img_bar_widt" ng-app="app" ng-controller="upload">
<form>
<div class="prev_img_arti-te_xt">
<input type="file" fileinput="file" filepreview="filepreview"/>
<img class="art_img_prev_use" ng-src="{{filepreview}}" ng-show="filepreview"/>
</div>
</form>
</div>
嘿,我嘗試進行簡單的圖像預覽的角度和寄託都似乎工作不錯,但有一個問題我解決不了。 問題:在上傳圖像後如何添加兩個按鈕「刪除圖像」和「更改圖像」,但我希望在添加圖像之前該按鈕不可見?
不幸的是,我認爲這不工作 –