Angularjs使用Monaca IDE和Onsen“TypeError:无法读取未定义的属性'getPicture'”

问题描述 投票:0回答:1

我正在使用Angularjs在Monaca IDE中编写移动应用程序。我正在尝试将图像附加到提交表单(拍照或包含文件中的图片)。

我有此错误消息

TypeError:无法读取undefined的属性'getPicture'。

我似乎无法弄清楚为什么它的'getPicture'是未定义的,我应该在项目的其他地方定义这个函数?

有关工作版本的任何建议吗?先感谢您

//My directive where getPicture() is called
<ons-button ng-click="getPicture()" />

//rebateFormCtrl Controller
.controller('rebateFormCtrl', function(rebate, $scope) {
    $scope.model = {};
    $scope.submit = function () {
        if ($scope.receipt == null) {
            ons.notification.alert({message: "Please attatch a picture of the receipt"});
        }
        rebate.submit($scope.model, $scope.receipt, function (err, rebate) {
            if (err) {
                angular.forEach(err, function (val, i) {
                    ons.notification.alert({title: i, message: val});
                });

            }
            else {
                console.log(rebate);
                $scope.rebates.current = rebate;
                $scope.myNavigator.pushPage('rebate.html');
            }
        });
    };
    $scope.getPicture = function () {
        navigator.camera.getPicture(
            function (imageData) {
                $scope.receipt = "data:image/jpeg;base64," + imageData;
            },
            function (msg) {
                console.log(msg);
            }
        );
    };
})

//rebate Factory
.factory("rebate", function ($http, Upload) {
    return {
        getById: function (id, callback) {
            $http.get(baseUrl + "rebates/" + id)
                .then(function (res) {
                    callback(null, res.data);
                },
                function (res) {
                    callback(res);
                });
        },
        submit: function (rebateData, receipt, callback) {
            var options = new FileUploadOptions();
            options.fileKey = "receipt";
            options.fileName = receipt.substr(receipt.lastIndexOf('/') + 1);
            options.mimeType = "image/jpeg";
            options.params = rebateData;

            var ft = new FileTransfer();
            ft.upload(receipt, encodeURI(baseUrl + 'rebates'),
                function (res) {
                  //success
                    console.log(JSON.stringify(res))
                    var rebate = JSON.parse(res.response);
                    callback(null, rebate);
                },
                function (res) {
                  //fail
                  console.log(JSON.stringify(res));
                  errors = JSON.parse(res.body);
                  callback(errors);
                }
            , options);
        }
    };
});
javascript jquery angularjs onsen-ui monaca
1个回答
0
投票

我假设您正在使用Cordova和Camera插件,错误告诉您无法在未定义的变量上调用getPicture()方法。

这意味着没有定义navigator.camera

您可以像下面的代码一样进行简单检查,看看它是否已设置。

if (typeof navigator.camera !== 'undefined') {
    //Execute your code here
}

如果你正在使用ngCordova(或Ionic),他们有一个很好的包装插件和Angular http://ngcordova.com/docs/plugins/camera/

© www.soinside.com 2019 - 2024. All rights reserved.