ionic.bundle.js:25642 Error: [$injector:unpr] Unknown provider: $cordovaGeolocationProvider <- $cordovaGeolocation <- AgeCtrl

六眼飞鱼酱① 提交于 2019-12-14 04:26:00

问题


I am currently in the starting phase of building an app via Ionic. Right now i want to implement the cordova geolocation in it. However this keeps giving an error when opening it. For testing purposes i use ionic serve and check it in localhost.

angular.module('starter', ['ionic','ionic.service.core', 'ui.router'])
.controller('AgeCtrl', function ($scope, $state, $http, $cordovaGeolocation) {


$scope.toggleItem = function (item) {
    item.checked = !item.checked;
};

$scope.items = [
  { id: '0-12' },
  { id: '12-18' },
  { id: '18-30' },
  { id: '30-65' },
  { id: '65+' }
];

    $scope.time = Date.now();
   $scope.weather = $http.get("http://api.openweathermap.org/data/2.5/weather?q=Amsterdam&units=metric&APPID=...").then(function(resp) {
   console.log("success", resp);
   }, function(err) {
    console.log("error");
   })

var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)

.then(function (position) {
  var lat  = position.coords.latitude
  var long = position.coords.longitude
  console.log(lat + '   ' + long)
}, function(err) {
  console.log(err)
});

$scope.Confirm = function (){
    $state.go('home');
}
})

Is there any place i have made a mistake which causes this problem?


回答1:


I think it's not referenced as $cordovaGeolocation in ionic. Try navigator.geolocation.getCurrentPosition instead?




回答2:


Ionic serve only emulates the application in the browser. You do not get access to the Cordova plugins within the browser.

To get the libraries included you need to run the app on the device after adding a specific platform depending on what device you have.

For iOS:

Ionic platform add ios

For android:

ionic platform add android

After the platforms are added you can build and run on device using the following command

For iOS:

ionic run iOS

For android:

ionic run android




回答3:


I believe the issue is with your ngCordova installation . do below steps -

1- install --------------> bower install ngCordova

2- include in index.html above cordova.js ------------------> script src="lib/ngCordova/dist/ng-cordova.js"

3- inject -------------> angular.module(starter, ['ngCordova'])

run IONIC serve and issue will be gone .. If it still shows error that /dist/ngCordova is not present then go to location manually where ng cordova is installed and copy it to the path e.g. - /lib/ngCordova/dist/...

it will definitely resolve your issue



来源:https://stackoverflow.com/questions/36157833/ionic-bundle-js25642-error-injectorunpr-unknown-provider-cordovageolocat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!