AngularJS从Sqlite删除所选图像
我有下面的代码,我可以从相机中拍摄图像,或者从gallery中选择图像,并将图像路径保存在sqlite上,然后显示它,当我尝试删除图像时,一切都很好执行它删除所有图像。 如何分别选择每个图像,然后按id或索引将其删除AngularJS从Sqlite删除所选图像,angularjs,sqlite,cordova,ionic-framework,Angularjs,Sqlite,Cordova,Ionic Framework,我有下面的代码,我可以从相机中拍摄图像,或者从gallery中选择图像,并将图像路径保存在sqlite上,然后显示它,当我尝试删除图像时,一切都很好执行它删除所有图像。 如何分别选择每个图像,然后按id或索引将其删除 var db = null; angular.module('starter', ['ionic', 'ngCordova']) .run(function($ionicPlatform, $cordovaSQLite) { $ionicPlatf
var db = null;
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
try {
db = $cordovaSQLite.openDB({
name: "my.db",
location: 'default'
});
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS imageTable " + "(id integer primary key, image text)");
} catch (e) {
alert(e);
} finally { }
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('ImageCtrl', function($scope, $rootScope, $state, $stateParams, $cordovaDevice, $cordovaFile, $ionicActionSheet, $cordovaCamera, $cordovaFile, $cordovaDevice, $ionicPopup, $cordovaActionSheet, $cordovaSQLite, $interval) {
var imagesP = [];
//$scope.images = [];
$scope.showAlert = function(title, msg) {
var alertPopup = $ionicPopup.alert({
title: title,
template: msg
});
};
$scope.loadImage = function() {
var options = {
title: 'Select Receipts Image ',
buttonLabels: ['Gallery', 'Take photo', 'File System'],
addCancelButtonWithLabel: 'Cancel',
androidEnableCancelButton: true,
};
$cordovaActionSheet.show(options).then(function(btnIndex) {
var type = null;
if (btnIndex === 1) {
type = Camera.PictureSourceType.PHOTOLIBRARY;
} else if (btnIndex === 2) {
type = Camera.PictureSourceType.CAMERA;
}
if (type !== null) {
$scope.selectPicture(type);
}
});
}
// Take image with the camera or from library and store it inside the app folder
// Image will not be saved to users Library.
$scope.selectPicture = function(sourceType) {
var options = {
quality: 75,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: sourceType,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
correctOrientation: true,
targetWidth: 800,
targetHeight: 800,
popoverOptions: CameraPopoverOptions, // for IOS and IPAD
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function(imagePath) {
// Grab the file name of the photo in the temporary directory
var currentName = imagePath.replace(/^.*[\\\/]/, '');
// alert(currentName);
//Create a new name for the photo to avoid duplication
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
//alert(newFileName);
// If you are trying to load image from the gallery on Android we need special treatment!
if ($cordovaDevice.getPlatform() == 'Android' && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
window.FilePath.resolveNativePath(imagePath, function(entry) {
window.resolveLocalFileSystemURL(entry, success, fail);
function fail(e) {
console.error('Error: ', e);
}
function success(fileEntry) {
var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
// Only copy because of access rights
$cordovaFile.copyFile(namePath, fileEntry.name, cordova.file.dataDirectory, newFileName).then(function(success) {
// $scope.image = newFileName;
var imgPath = cordova.file.dataDirectory + newFileName;
$scope.add(imgPath);
}, function(error) {
$scope.showAlert('Error', error.exception);
});
// alert( fileEntry.nativeURL);
};
});
} else {
var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
// Move the file to permanent storage
$cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success) {
// $scope.image = newFileName;
//$scope.images.push(newFileName);
var imgPath = cordova.file.dataDirectory + newFileName;
$scope.add(imgPath);
}, function(error) {
$scope.showAlert('Error', error.exception);
});
}
},
function(err) {
// Not always an error, maybe cancel was pressed...
})
};
$scope.add = function(path) {
if (imagesP != null) {
$cordovaSQLite.execute(db, "INSERT INTO imageTable (images) VALUES(?)", [path] );
}
alert("Inserted.");
},
function(e) {
alert(e);
};
$scope.delete = function(id) {
if (id != '') {
$cordovaSQLite.execute(db, "DELETE FROM imageTable WHERE id=?", [id]);
}
alert("Deleted.");
$scope.ShowAllData();
},
function(e) {
alert(e);
};
$scope.ShowAllData = function() {
$scope.images = [];
$cordovaSQLite.execute(db,"SELECT images FROM imageTable").then(function(res) {
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
$scope.images.push({
id: res.rows.item(i).id,
image: res.rows.item(i).images
});
}
}
}, function(error) {
alert(error);
} );
return $scope.images;
}
//$scope.ShowAllData();
//$interval($scope.ShowAllData, 2000,1);
// Returns the local path inside the app for an image
$scope.pathForImage = function() {
return cordova.file.dataDirectory + $scope.ShowAllData();
};
});
var db=null;
角度模块('starter'、['IONAL'、'ngCordova']))
.run(函数($ionicPlatform,$cordovaSQLite){
$ionicPlatform.ready(函数(){
试试{
db=$cordovaSQLite.openDB({
名称:“my.db”,
位置:“默认”
});
$cordovaSQLite.execute(db,“如果不存在创建表imageTable”+“(id整数主键,图像文本)”)
}第(e)款{
警报(e)
}最后{}
if(window.cordova&&window.cordova.plugins.Keyboard){
插件键盘hideKeyboardAccessoryBar(真);
插件。键盘。禁用滚动(真);
}
如果(窗口状态栏){
StatusBar.styleDefault();
}
});
})
.controller('ImageCtrl',函数($scope、$rootScope、$state、$stateParams、$cordovaDevice、$cordovaFile、$IoniActionSheet、$cordovaCamera、$cordovaFile、$cordovaDevice、$ionicPopup、$cordovaActionSheet、$cordovaSQLite、$interval){
var imagesP=[];
//$scope.images=[];
$scope.showAlert=函数(标题,消息){
var alertPopup=$ionicPopup.alert({
标题:标题,,
模板:msg
});
};
$scope.loadImage=function(){
变量选项={
标题:“选择收据图像”,
按钮标签:[“画廊”、“拍照”、“文件系统”],
addCancelButtonWithLabel:“Cancel”,
androidEnableCancelButton:没错,
};
$cordovaActionSheet.show(选项)。然后(函数(btnIndex){
var type=null;
如果(btnIndex==1){
类型=Camera.PictureSourceType.PHOTOLIBRARY;
}else if(btnIndex==2){
类型=Camera.PictureSourceType.Camera;
}
如果(类型!==null){
$scope.选择图片(类型);
}
});
}
//使用相机或从库中拍摄图像,并将其存储在应用程序文件夹中
//图像将不会保存到用户库。
$scope.selectPicture=函数(sourceType){
变量选项={
质量:75,
destinationType:Camera.destinationType.FILE\u URI,
sourceType:sourceType,
允许:是的,
编码类型:Camera.encodingType.JPEG,
对,,
目标宽度:800,
目标:800,
popoverOptions:CamerapoverOptions,//适用于IOS和IPAD
saveToPhotoAlbum:false
};
$cordovaCamera.getPicture(选项)。然后(函数(imagePath){
//在临时目录中获取照片的文件名
var currentName=imagePath.replace(/^.[\\/]/,“”);
//警报(当前名称);
//为照片创建新名称以避免重复
var d=新日期(),
n=d.getTime(),
newFileName=n+“.jpg”;
//警报(newFileName);
//如果您试图从Android上的图库加载图像,我们需要特殊处理!
if($cordovaDevice.getPlatform()='Android'&&sourceType===Camera.PictureSourceType.PHOTOLIBRARY){
window.FilePath.ResolvePath(imagePath,函数(条目){
resolveLocalFileSystemURL(条目、成功、失败);
功能失效(e){
console.error('error:',e);
}
函数成功(fileEntry){
var namePath=fileEntry.nativeURL.substr(0,fileEntry.nativeURL.lastIndexOf('/')+1);
//仅复制,因为具有访问权限
$cordovaFile.copyFile(namePath,fileEntry.name,cordova.file.dataDirectory,newFileName)。然后(函数(成功){
//$scope.image=newFileName;
var imgPath=cordova.file.dataDirectory+newFileName;
$scope.add(imgPath);
},函数(错误){
$scope.showAlert('Error',Error.exception);
});
//警报(fileEntry.nativeURL);
};
});
}否则{
var namePath=imagePath.substr(0,imagePath.lastIndexOf('/')+1);
//将文件移动到永久存储
$cordovaFile.moveFile(namePath,currentName,cordova.file.dataDirectory,newFileName)。然后(函数(成功){
//$scope.image=newFileName;
//$scope.images.push(newFileName);
var imgPath=cordova.file.dataDirectory+newFileName;
$scope.add(imgPath);
},函数(错误){
$scope.showAlert('Error',Error.exception);
<body ng-app="starter" ng-controller="ImageCtrl">
<ion-pane>
<ion-header-bar class="bar-positive">
<h1 class="title"> Image Upload</h1>
</ion-header-bar>
<ion-content ng-repeat="image in images track by index">
<img ng-src="{{image.image}}" style="width: 100%; height: 100%;">
</ion-content>
<ion-footer-bar class="bar bar-positive">
<div class="button-bar">
<button class="button icon-left ion-camera" ng-click="loadImage()">Take Photo</button>
<button class="button icon-left ion-camera" ng-click="ShowAllData()">show Photo</button>
<button class="button icon-left ion-camera" ng-click="delete($index,image.id)">Delete Photo</button>
</div>
</ion-footer-bar>
</ion-pane>
</body>
<body ng-app="starter" ng-controller="ImageCtrl">
<ion-pane>
<ion-header-bar class="bar-positive">
<h1 class="title"> Image Upload</h1>
</ion-header-bar>
<ion-content ng-repeat="image in images track by index">
<img ng-src="{{image.image}}" style="width: 100%; height: 100%;">
<button class="button icon-left ion-camera" ng-click="delete(image.id)">Delete Photo</button>
</ion-content>
<ion-footer-bar class="bar bar-positive">
<div class="button-bar">
<button class="button icon-left ion-camera" ng-click="loadImage()">Take Photo</button>
<button class="button icon-left ion-camera" ng-click="ShowAllData()">show Photo</button>
</div>
</ion-footer-bar>
</ion-pane>
</body>