Angular:将数据从服务/工厂传递给控制器

时间:2015-08-15 17:31:54

标签: javascript angularjs ionic-framework ionic angular-promise

我有一个控制器,其中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.selectPicture

0 个答案:

没有答案