Angularjs 使用ionicplatform.ready with camera功能时,$digest已在进行中
这是我的控制器,我正试图用第1页的一个按钮捕获图像并将其存储在本地,然后在第1页显示image1,如果我再次单击该按钮以拍摄image2的图片。Image2应显示在第1页,image1应显示在第2页Angularjs 使用ionicplatform.ready with camera功能时,$digest已在进行中,angularjs,ionic-framework,Angularjs,Ionic Framework,这是我的控制器,我正试图用第1页的一个按钮捕获图像并将其存储在本地,然后在第1页显示image1,如果我再次单击该按钮以拍摄image2的图片。Image2应显示在第1页,image1应显示在第2页 .controller('LeadDetailController', [ '$scope', '$cordovaDevice', '$cordovaFile', '$ionicPlatform',
.controller('LeadDetailController', [
'$scope',
'$cordovaDevice',
'$cordovaFile',
'$ionicPlatform',
'ImageService', 'FileService',
function( $scope,
$cordovaDevice,
$cordovaFile,
$ionicPlatform,
ImageService, FileService) {
// image capture code
$ionicPlatform.ready(function() {
console.log('ionic is ready');
$scope.images = FileService.images();
$scope.$apply();
});
$scope.urlForImage = function(imageName) {
var trueOrigin = cordova.file.dataDirectory + imageName;
return trueOrigin;
}
$scope.addImage = function(type) {
ImageService.handleMediaDialog(type).then(function() {
$scope.$apply();
});
}
在初始阶段,我得到了这个错误
错误:[$rootScope:inprog]$digest已在进行中
带按钮的第1页
// here camera function is called to open the camera and take pic
<ion-option-button ng-click="addImage()"class="icon ion-android-camera"></ion-option-button>
//here the pic taken in camera should be displayed
<ion-option-button>
<img src="">
</ion-option-button>
//here moveing to the next page2
<ion-option-button ng-click="Page2()" class="icon ion-ios-grid-view"></ion-option-button>
服务
.factory('FileService', function() {
var images;
var IMAGE_STORAGE_KEY = 'images';
function getImages() {
var img = window.localStorage.getItem(IMAGE_STORAGE_KEY);
if (img) {
images = JSON.parse(img);
} else {
images = [];
}
return images;
};
function addImage(img) {
images.push(img);
window.localStorage.setItem(IMAGE_STORAGE_KEY, JSON.stringify(images));
};
return {
storeImage: addImage,
images: getImages
}
})
.factory('ImageService', function($cordovaCamera, FileService, $q, $cordovaFile) {
function makeid() {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
function optionsForType(type) {
var source;
/* switch (type) {
case 0:
source = Camera.PictureSourceType.CAMERA;
break;
case 1:
source = Camera.PictureSourceType.PHOTOLIBRARY;
break;
}*/
return {
destinationType: Camera.DestinationType.FILE_URI,
sourceType: source,
allowEdit: false,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
}
function saveMedia(type) {
return $q(function(resolve, reject) {
var options = optionsForType(type);
$cordovaCamera.getPicture(options).then(function(imageUrl) {
var name = imageUrl.substr(imageUrl.lastIndexOf('/') + 1);
var namePath = imageUrl.substr(0, imageUrl.lastIndexOf('/') + 1);
var newName = makeid() + name;
$cordovaFile.copyFile(namePath, name, cordova.file.dataDirectory, newName)
.then(function(info) {
FileService.storeImage(newName);
resolve();
}, function(e) {
reject();
});
});
})
}
return {
handleMediaDialog: saveMedia
}
});
.factory('FileService',function()){
var图像;
var IMAGE_STORAGE_KEY='images';
函数getImages(){
var img=window.localStorage.getItem(IMAGE\u STORAGE\u KEY);
如果(img){
images=JSON.parse(img);
}否则{
图像=[];
}
返回图像;
};
功能添加图像(img){
图像推送(img);
setItem(IMAGE_STORAGE_KEY,JSON.stringify(images));
};
返回{
storeImage:addImage,
图片:getImages
}
})
.factory('ImageService',函数($cordovaCamera,FileService,$q,$cordovaFile){
函数makeid(){
var text='';
可能的变量='ABCDEFGHIjklmnopqrstuvxyzabCDEFGHIjklmnopqrstuvxyzo123456789';
对于(变量i=0;i<5;i++){
text+=可能的.charAt(Math.floor(Math.random()*可能的.length));
}
返回文本;
};
功能选项FORTYPE(类型){
var源;
/*开关(类型){
案例0:
source=Camera.PictureSourceType.Camera;
打破
案例1:
source=Camera.PictureSourceType.PHOTOLIBRARY;
打破
}*/
返回{
destinationType:Camera.destinationType.FILE\u URI,
sourceType:source,
允许:错误,
编码类型:Camera.encodingType.JPEG,
popoverOptions:CamerapoverOptions,
saveToPhotoAlbum:false
};
}
功能存储媒体(类型){
返回$q(函数(解析、拒绝){
var选项=选项FORTYPE(类型);
$cordovaCamera.getPicture(选项)。然后(函数(imageUrl){
var name=imageUrl.substr(imageUrl.lastIndexOf('/')+1);
var namePath=imageUrl.substr(0,imageUrl.lastIndexOf('/')+1);
var newName=makeid()+name;
$cordovaFile.copyFile(名称路径、名称、cordova.file.dataDirectory、newName)
.然后(功能(信息){
FileService.storeImage(新名称);
解决();
},功能(e){
拒绝();
});
});
})
}
返回{
handleMediaDialog:saveMedia
}
});
是否有人可以帮助我解决此问题,并帮助我处理@Ila所述的从第2页到图像查看的问题。此问题可能是由$scope.$apply()引起的 因此,如果无法预测是否必须使用它,请按如下方式插入if语句:
if(!$scope.$$phase) {
// no digest in progress...
$scope.$apply();
}
但是,这不是一个好的做法:只在真正需要时使用$scope.$apply()。如@Ila所述,问题可能是由于$scope.$apply()引起的 因此,如果无法预测是否必须使用它,请按如下方式插入if语句:
if(!$scope.$$phase) {
// no digest in progress...
$scope.$apply();
}
但是,这不是一个好的做法:只在真正需要时使用$scope.$apply()。参见我同意vb平台。但您也可以将代码包装成
$timeout
,这样angularjs
就可以为您处理$apply
:
我同意vb平台。但您也可以将代码包装成
$timeout
,这样angularjs
就可以为您处理$apply
:
尝试不使用$scope。$apply()是否有人可以帮助我在另一页中显示图像尝试不使用$scope。$apply()是否有人可以帮助我在另一页中显示图像
if(!$scope.$$phase) {
// no digest in progress...
$scope.$apply();
}
$ionicPlatform.ready(function() {
console.log('ionic is ready');
$timeout(function setImages() {
$scope.images = FileService.images();
});
});