角度:将数据从服务/工厂传递到控制器

Ahmad Elmoualem |

我有一个控制器,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);
                });
};

但是,似乎没有正确执行此操作,因为出现此错误:

无法在Scope。$ scope.selectPicture中读取未定义的属性'then'

潘卡·帕克(Pankaj Parkar)

您的服务应该返回承诺,因为$cordovaCamera.getPicture确实返回了承诺,不需要$ionicPlatform.ready(function()方法。

代码

function takePicture () {
   var img = $cordovaCamera.getPicture(options);
   return img;
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章