[英]Angularjs using Monaca IDE and Onsen “TypeError: Cannot read property 'getPicture' of undefined”
I'm writing a mobile app in the Monaca IDE, using Angularjs. 我正在使用Angularjs在Monaca IDE中编写一个移动应用程序。 I'm trying to attach an image to a submit form (take picture, or include picture from file). 我正在尝试将图像附加到提交表单(拍摄图片或包括文件中的图片)。
I have this error message 我有此错误信息
TypeError: Cannot read property 'getPicture' of undefined. TypeError:无法读取未定义的属性“ getPicture”。
I can't seem to figure out why its saying 'getPicture' is undefined, where else in the project should I define this function? 我似乎无法弄清楚为什么它的“ getPicture”一词未定义,我应该在项目中的其他地方定义此函数吗?
Any suggestions on a working version? 对工作版本有任何建议吗? Thank you in advance 先感谢您
//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);
}
};
});
I'm assuming you're using Cordova and the Camera plugin, the error is telling you that the getPicture()
method cannot be called on an undefined variable. 我假设您使用的是Cordova和Camera插件,该错误告诉您无法在未定义的变量上调用getPicture()
方法。
Which means that navigator.camera
is not defined. 这意味着未定义navigator.camera
。
You can do a simple check like the following code to see if it's set or not. 您可以像以下代码一样进行简单的检查,以查看是否已设置。
if (typeof navigator.camera !== 'undefined') {
//Execute your code here
}
If you are using ngCordova (or Ionic) they have a nice wrapper for this plugin and Angular http://ngcordova.com/docs/plugins/camera/ 如果您使用的是ngCordova(或Ionic),则该插件和Angular都有一个不错的包装器http://ngcordova.com/docs/plugins/camera/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.