好吧,我正在研究一个用离子法制作的小项目。我正在使用ngCordova添加一些很酷的插件,比如指南针的设备方向。但我不能强迫它发挥作用。我已经安装了ngCordova并添加了插件(地理位置插件工作)。我没有想到为什么这样做不起作用。
代码如下所示:
.controller('GeoLocationCtrl', function($scope, $cordovaGeolocation, $cordovaDeviceOrientation, $ionicPlatform, $document) {
$scope.lat = 0;
$scope.long = 0;
$scope.position = {};
$ionicPlatform.ready(function() {
var options = {frequency: 1000}; // Update every 1 second
var watch2 = $cordovaDeviceOrientation.watchHeading(options); ///LINE 53!
watch2.promise.then(function (result) { /* unused */
},
function (err) {
// An error occurred
}, function (position) {
// Heading comes back in
// position.magneticHeading
$scope.position = position.magneticHeading;
});
$cordovaDeviceOrientation.clearWatch(watch2.watchId)
.then(function (result) {
// Success!
}, function (err) {
// An error occurred
});
});
})
但我得到的只是:
ripple.js:37
TypeError: Cannot read property 'watchHeading' of undefined
at Object.watchHeading (http://localhost:8080/lib/ngCordova/dist/ng- cordova.min.js:1:14565)
at http://localhost:8080/js/controllers.js:53:44
at http://localhost:8080/lib/ionic/js/ionic.bundle.js:37388:19
at Object.ionic.Platform.ready (http://localhost:8080/lib/ionic/js/ionic.bundle.js:1915:9)
at Object.self.ready (http://localhost:8080/lib/ionic/js/ionic.bundle.js:37386:26)
at new <anonymous> (http://localhost:8080/js/controllers.js:15:20)
at invoke (http://localhost:8080/lib/ionic/js/ionic.bundle.js:11591:17)
at Object.instantiate (http://localhost:8080/lib/ionic/js/ionic.bundle.js:11602:23)
at http://localhost:8080/lib/ionic/js/ionic.bundle.js:14906:28
at updateView (http://localhost:8080/lib/ionic/js/ionic.bundle.js:42986:30) ionic.bundle.js:17696
感谢您的帮助!