Angular:将数据从 service/factory 传递给控制器
Angular: pass data from service/factory to controller
我有一个控制器,其中 selectPicture
funtion
用于拍照并在视图中使用此图片。
这是控制器中函数的代码:
$scope.selectPicture = function() {
document.addEventListener('deviceready', function() {
var options = {
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
correctOrientation: true,
targetWidth: 720,
};
$cordovaCamera.getPicture(options).then(function(imageURI) {
$scope.imageSrc = imageURI;
$scope.img = imageURI;
}, function(err) {
alert(err);
});
}, false); // device ready
}; // Select picture
控制器代码变得混乱所以我想将相机的逻辑投入使用我做了以下 cameraService
:
.factory('cameraService', function($cordovaCamera, $ionicPlatform) {
var options = {
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
correctOrientation: true,
targetWidth: 720,
};
function takePicture () {
$ionicPlatform.ready(function() {
var img = $cordovaCamera.getPicture(options);
return img;
});
};
return {
takePicture: takePicture
};
});
另外我在注入服务后将控制器中的代码固定为这样:
$scope.selectPicture = function() {
cameraService.takePicture().then(function(imageURI) {
$scope.imageSrc = imageURI;
$scope.img = imageURI;
}, function(err) {
alert(err);
});
};
但是,似乎我做的不正确,因为我得到这个错误:
Cannot read property 'then' of undefined at Scope.$scope.selectPicture
那是因为 takePicture
没有 return 承诺(then
是承诺方法)。所以:
// service
function takePicture () {
return $cordovaCamera.getPicture(options);
};
// controller
$scope.selectPicture = function() {
cameraService.takePicture().then(function(imageURI) {
$scope.img = imageURI;
});
}
$ionicPlatform.ready(function() {
// initialization?
});
好的,看来 getPicture
已经 return 是一个承诺。所以你只需要 return 它。由于 $scope.selectPicture
是用户在单击或点击时触发的内容,因此您无需在每次需要拍照时都调用 $ionicPlatform.ready
。
您的服务应该 return 承诺,因为 $cordovaCamera.getPicture
确实 return 承诺,不需要 $ionicPlatform.ready(function()
方法。
代码
function takePicture () {
var img = $cordovaCamera.getPicture(options);
return img;
});
我有一个控制器,其中 selectPicture
funtion
用于拍照并在视图中使用此图片。
这是控制器中函数的代码:
$scope.selectPicture = function() {
document.addEventListener('deviceready', function() {
var options = {
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
correctOrientation: true,
targetWidth: 720,
};
$cordovaCamera.getPicture(options).then(function(imageURI) {
$scope.imageSrc = imageURI;
$scope.img = imageURI;
}, function(err) {
alert(err);
});
}, false); // device ready
}; // Select picture
控制器代码变得混乱所以我想将相机的逻辑投入使用我做了以下 cameraService
:
.factory('cameraService', function($cordovaCamera, $ionicPlatform) {
var options = {
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
correctOrientation: true,
targetWidth: 720,
};
function takePicture () {
$ionicPlatform.ready(function() {
var img = $cordovaCamera.getPicture(options);
return img;
});
};
return {
takePicture: takePicture
};
});
另外我在注入服务后将控制器中的代码固定为这样:
$scope.selectPicture = function() {
cameraService.takePicture().then(function(imageURI) {
$scope.imageSrc = imageURI;
$scope.img = imageURI;
}, function(err) {
alert(err);
});
};
但是,似乎我做的不正确,因为我得到这个错误:
Cannot read property 'then' of undefined at Scope.$scope.selectPicture
那是因为 takePicture
没有 return 承诺(then
是承诺方法)。所以:
// service
function takePicture () {
return $cordovaCamera.getPicture(options);
};
// controller
$scope.selectPicture = function() {
cameraService.takePicture().then(function(imageURI) {
$scope.img = imageURI;
});
}
$ionicPlatform.ready(function() {
// initialization?
});
好的,看来 getPicture
已经 return 是一个承诺。所以你只需要 return 它。由于 $scope.selectPicture
是用户在单击或点击时触发的内容,因此您无需在每次需要拍照时都调用 $ionicPlatform.ready
。
您的服务应该 return 承诺,因为 $cordovaCamera.getPicture
确实 return 承诺,不需要 $ionicPlatform.ready(function()
方法。
代码
function takePicture () {
var img = $cordovaCamera.getPicture(options);
return img;
});