首先咱們須要一個指令來追蹤input的change。ngChage不適用input[file]。html
app.directive("fileread", [function () {
return {
scope: {
selectedFile: "=",
changed: '&'
},
link: function(scope, element, attributes) {
element.bind("change", function(changeEvent) {
scope.$apply(function() {
scope.selectedFile = changeEvent.target.files[0];
// or all selected files:
// scope.fileread = changeEvent.target.files;
console.log('file selected.');
if (scope.changed()) {
scope.changed()(scope.selectedFile);
}
});
});
}
};
}]);app
而後在controller裏定義file的變量跟change綁定的function。post
$scope.showFileSelectBox = function () {
$("#imgSelectInput").click();
};this
$scope.imageSelected = function(file) {
var image;
if (file) {
image = new Image();
image.onload = function () {htm
$scope.editObj.Width = this.width;
$scope.editObj.Height = this.height;
};
image.src = $window.URL.createObjectURL(file);
}
};blog
而後是htmlelement
<button type="button" ng-click="showFileSelectBox()">上傳</button>
<input type="file" style="display: none" accept="image/*" fileread selectedfile="selectedImgFile" id="imgSelectInput" changed="imageSelected" />get