1

ユーザーの現在の場所を取得するためにファクトリ メソッドを使用しています。以下は私のファクトリーメソッドです:

app.factory('FactoryName', function($http, $q, $window){ 
    'use strict';
    function getCurrentPosition() {
        var deferred = $q.defer();

        if (!$window.navigator.geolocation) {
            deferred.reject('Geolocation not supported.');
        } else {
            $window.navigator.geolocation.getCurrentPosition(
                function (position) {
                    deferred.resolve(position);
                },
                function (err) {
                    deferred.reject(err);
                });
        }

        return deferred.promise;
    }    

    return {
        getCurrentPosition: getCurrentPosition
    };      
});

コントローラー内で、次のようにユーザーの現在の場所を取得しています。

FactoryName.getCurrentPosition().then(UserLocationFound).catch(UserLocationNotFound);
var UserLocationFound = function(data, status) {
   $scope.latitude = data.coords.latitude;
   $scope.longitude = data.coords.longitude;
};
var UserLocationNotFound = function(data, error) { 
   console.log("location not found")
};

このページの URL が "domain.com/form" で、このページに直接アクセスすると、問題なく動作すると、状況に応じて "then" と "catch" が発生します。しかし、他のページ(アンカータグのリンク)からこのページに移動すると問題が発生します。この場合、どのイベントも発生しません。あるケースでは機能し、他のケースでは機能しない理由を理解できません。

4

0 に答える 0