I have the same functionality in two controllers and I want to refactor to place it in a shared method that both controllers can access. I was thinking on creating a separate service called "Camera" and inject that service as a dependecy into both controllers as I'm currently doing with "Login". Is this a good approach?
.controller('pictureOfTheDamageCtrl', function ($scope, $cordovaCamera, $ionicPopup, $rootScope, $timeout, Login, $ionicLoading) {
$rootScope.width = document.getElementsByTagName('ion-content')[0].clientWidth;
$scope.takePicture = function () {
var options = {
quality: 75,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: false,
targetWidth: 1024,
targetHeight: 567,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function (imageURI) {
$rootScope.damage = "data:image/jpeg;base64," + imageURI;
}, function (err) {
$ionicPopup.alert({
title: 'Camera',
template: 'Something went wrong: ' + err
});
});
}
})
.controller('pictureOfTheVehicleCtrl', function ($scope, $cordovaCamera, $ionicPopup, $rootScope, $timeout, Login, $ionicLoading) {
$rootScope.width = document.getElementsByTagName('ion-content')[0].clientWidth;
$scope.takePicture = function () {
var options = {
quality: 75,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: false,
targetWidth: 1024,
targetHeight: 567,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function (imageURI) {
$rootScope.vehicle = "data:image/jpeg;base64," + imageURI;
}, function (err) {
$ionicPopup.alert({
title: 'Camera',
template: 'Something went wrong: ' + err
});
});
}
})