未知提供商:$ cordovaGeolocationProvider

raz*_*zak 1 cordova ionic-framework

我目前正处于通过Ionic构建应用程序天气的起始阶段.现在我想实现cordova地理定位,我有错误Unknown provider: $cordovaGeolocationProvider <- $cordovaGeolocation我尝试修复但没有成功,但是这在打开时不断给出错误.出于测试目的,我使用离子服务并在localhost中检查它.

我的代码

(function() {
    angular.module('app.weather', ['ionic'])

        .factory('Weather', function($q, $http) {
            var deferred = $q.defer();

            function getCurrentWeather(lat, lng) {
                var url = 'https://api.forecast.io/forecast//' + lat + ',' + lng + '?callback=JSON_CALLBACK';
                $http.jsonp(url)
                    .success(deferred.resolve)
                    .error(deferred.reject);

                return deferred.promise;
            }

            return {
                getCurrentWeather: getCurrentWeather
            };
        })
        .controller('WeatherCtrl', function($scope, $cordovaGeolocation, Weather) {
            $scope.loading = true;

            $scope.toCelsius = function(temperature) {
                return ((temperature - 32) / 1.8).toFixed(1);
            };

            $cordovaGeolocation
                .getCurrentPosition({
                    timeout: 10000,
                    enableHighAccuracy: false
                })
                .then(function(position) {
                    var lat = position.coords.latitude;
                    var long = position.coords.longitude;

                    Weather.getCurrentWeather(lat, long).then(function(data) {
                        $scope.weatherInfo = data;
                        $scope.loading = false;
                    }, function(error) {
                        //TODO Display error message
                    });
                }, function(err) {
                    //TODO Display error message
                });
        });
})();
Run Code Online (Sandbox Code Playgroud)

代码html

<ion-view title="????????" id="page2" style="background-color:#FFFFFF;" class=" ">
    <ion-content padding="true" class="has-header">
    <ion-pane>
<div ng-controller="WeatherCtrl as weatherCtrl">
 <div ng-show="loading">
                Carregando informações...
            </div>
            <div ng-hide="loading">
                <p>
                    Temperatura: {{toCelsius(weatherInfo.currently.temperature)}}º
                </p>
                <p>
                    Sensação térmica: {{toCelsius(weatherInfo.currently.apparentTemperature)}}º
                </p>
            </div>
                        </div>

    </ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)

Gop*_*pan 8

在你的模块中添加'ngCordova'就像

(function() {
    angular.module('app.weather', ['ionic','ngCordova'])
Run Code Online (Sandbox Code Playgroud)

然后将ngcordova.min.js文件链接添加到index.html