0
我有一個數組添加圖像,我看到圖像顯示在縮略圖下面,當我添加圖像時,我想從數組中刪除選定的圖像。從陣列中刪除圖像
我有一個名爲刪除圖像功能,我試圖讓一些工作,但我需要在陣列中選擇任何圖像,並刪除(如X旁邊的圖像)
HTML
<img style="height: 100px; width: 100px" ng-src="{{preview}}" alt="preview image">
<label for="file">Select File</label>
<input ng-model="file" type='file' ng-model-instant name='file' id='fileinput' accept='image/*' onchange='angular.element(this).scope().uploadImage(this)' /> {{uploadError}}
<button ng-click="addImage()">Add image</button>
<div ng-repeat="slot in slots">
<img ng-click="addImage()" style="height: 100px; width: 100px" ng-src="{{slot.base_image}}" alt="preview image">
</div>
的JavaScript
$scope.preview = 'img/download.png';
$scope.slots = [];
$scope.maxSlots = 5; // this dynamic
$scope.uploadImage = function() {
// console.log('we are here');
input = document.getElementById('fileinput');
file = input.files[0];
size = file.size;
if (size < 650000) {
var fr = new FileReader;
fr.onload = function (e) {
var img = new Image;
img.onload = function() {
var width = img.width;
var height = img.height;
if (width == 1920 && height == 1080) {
// console.log(e.target.result);
$scope.preview = e.target.result;
$scope.perfect = "you added a image";
// window.alert("perfect");
$scope.$apply();
} else {
$scope.notPerfect = "incorrect definitions";
// console.log(width, height);
$scope.$apply();
}
};
img.src = fr.result;
};
fr.readAsDataURL(file);
} else {
// window.alert("to big");
$scope.notPerfect = "to big";
// console.log('file size to big')
}
};
$scope.addImage = function() {
if ($scope.slots.length < $scope.maxSlots) {
$scope.slots.push({
"slot_id": $scope.slots.length + 1,
"base_image": $scope.preview,
"path_image": ""
});
} else {
window.alert("you have to delete a slot to generate a new one");
// console.log('you have to delete a slot to generate a new one')
}
};
$scope.removeImage = function() {
if ($scope.slots.length < $scope.maxSlots) {
$scope.slots.push({
"slot_id": $scope.slots.length - 1,
"base_image": $scope.preview,
"path_image": ""
});
} else {
window.alert("you have to delete a slot to generate a new one");
// console.log('you have to delete a slot to generate a new one')
}
};
'<按鈕納克單擊= 「removeImage(時隙)」>','$ scope.removeImage =函數(S){$ scope.slots。拼接($ scope.slots.indexOf(s),1); };'。類似的東西。你應該通過圖片的參考去除,以便你可以在你的收藏中輕鬆檢測到它。檢查數組拼接:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – briosheje
@briosheje謝謝,我試試這個 – Beep
作品完美謝謝兄弟! – Beep