【问题标题】:Unknown provider: $cordovaGeolocationProvider未知提供者:$cordovaGeolocationProvider
【发布时间】:2016-07-29 18:29:50
【问题描述】:

我目前正处于通过 Ionic 构建应用程序天气的开始阶段。现在我想实现科尔多瓦地理定位,我有错误Unknown provider: $cordovaGeolocationProvider <- $cordovaGeolocation我尝试修复但没有成功,但是打开它时会一直出错。出于测试目的,我使用 ionic serve 并在 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
                });
        });
})();

代码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>

【问题讨论】:

标签: cordova ionic-framework


【解决方案1】:

在你的模块中添加“ngCordova”

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

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

【讨论】:

    【解决方案2】:

    确保您已安装ngCordova 或Ionic Native,否则您不能使用$cordovaGeolocation,因为它是在这些库之一中定义的包装器。

    您可以找到有关如何安装 ngCordova here 和 here for Ionic Native 的说明。最近还有一篇关于Ionic Nativehere 的文章可能很有趣。

    另外请记住,大多数 Cordova 插件无法在浏览器中运行,因此您可能需要在实际设备上进行测试。

    【讨论】:

      【解决方案3】:

      您是否安装了 $cordovaGeolocation 插件?

      $cordova地理位置描述:http://ngcordova.com/docs/plugins/geolocation/

      如果没有,您可以通过以下方式安装它: cordova plugin add cordova-plugin-geolocation

      【讨论】:

        【解决方案4】:

        $cordovaGeolocation 功能以及任何其他本机功能只能在模拟器或设备上使用。如果您从浏览器提供应用程序,则它无法与插件交互,因此未定义。

        【讨论】:

          猜你喜欢
          • 2016-07-09
          • 1970-01-01
          • 2013-09-27
          • 2016-11-29
          • 2018-09-28
          • 2014-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多