我正在尝试制作一个简单的应用程序来使用手机的相机。问题是传递给Camera
的{{1}}对象是空的:
CameraCtrl
当我按下"拍照"按钮,我得到以下控制台日志:
0 347478日志拍照... 1 347482 log {} 2 347507错误TypeError:无法读取属性' DATA_URL'未定义的 在Scope。$ scope.takePicture(http://192.168.1.105:8100/js/controllers.js:11:52) at fn(eval at(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:26457:15),:4:224) 在http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:62386:9 在范围。$ eval(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:29158:28) 在Scope。$ apply(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:29257:23) 在HTMLButtonElement。 (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:62385:13) 在HTMLButtonElement.eventHandler(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:16583:21) 在triggerMouseEvent(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:2948:7) 在tapClick(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:2937:3) 在HTMLDocument.tapTouchEnd(http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:3064:5)
以下是我的angular.module('app.controllers', [])
.controller('CameraCtrl', function($scope, Camera){
$scope.pictureUrl = "http://lorempixel.com/400/200/";
$scope.takePicture = function() {
console.log('taking picture...');
console.log(JSON.stringify(Camera));
var options = {
destinationType: Camera.DestinationType.DATA_URL,
encodingType: Camera.EncodingType.JPEG
};
Camera.getPicture(options)
.then(
function(data){
$scope.pictureUrl = "data:image/jpeg;base64," + data;
},
function(error){
});
};
})
文件的内容:
services.js
angular.module('app.services', [])
.factory('BlankFactory', [function(){
}])
.factory('Camera', ['$q', function($q) {
return {
getPicture: function(options) {
var q = $q.defer();
navigator.camera.getPicture(function(result) {
q.resolve(result);
}, function(err) {
q.reject(err);
}, options);
return q.promise;
}
};
}])
.service('BlankService', [function(){
}]);
档案的开头:
controllers.js
angular.module('app.controllers', [])
.controller('CameraCtrl', function($scope, Camera){
$scope.pictureUrl = "http://lorempixel.com/400/200/";
$scope.takePicture = function() {
console.log('taking picture...');
console.log(JSON.stringify(Camera));
var options = {
destinationType: Camera.DestinationType.DATA_URL,
encodingType: Camera.EncodingType.JPEG
};
Camera.getPicture(options)
.then(
function(data){
$scope.pictureUrl = "data:image/jpeg;base64," + data;
},
function(error){
});
};
})
文件:
app.js
angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.directives','app.services',])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
});
文件:
index.html
答案 0 :(得分:1)
看起来您必须重命名您的相机工厂,因为该名称与插件冲突。
因此,当您致电Camera.DestinationType
时,您的控制器中会出现这种情况,因为您认为您正在尝试呼叫名为Camera
的工厂