【问题标题】:Reuse methods within one service in angular.js在 angular.js 中的一项服务中重用方法
【发布时间】:2013-07-16 17:01:14
【问题描述】:

我正在使用带有 phonegap 的 angular.js。为了获取用户 gps 位置,我开发了一个简单的服务:

app.factory('geolocation', function ($rootScope, cordovaReady) {
    return {
        getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {

            navigator.geolocation.getCurrentPosition(function () {
                var that = this,
                    args = arguments;

                if (onSuccess) {
                    $rootScope.$apply(function () {
                        onSuccess.apply(that, args);
                    });
                }
            }, function () {
                var that = this,
                    args = arguments;
                if (onError) {
                    $rootScope.$apply(function () {
                        onError.apply(that, args);
                    });
                }
            }, options);
        });
    }
});

我这样称呼这个服务

geolocation.getCurrentPosition(function (position) {
   console.log(position);
});

现在我想使用另一个名为 getCurrentCity 的方法来扩展服务。此方法应使用 getCurrentPosition 来确定城市。所以服务看起来像这样:

app.factory('geolocation', function ($rootScope, cordovaReady) {
    return {
        getCurrentPosition: ....
        getCurrentCity: ...
    }
});

这可能吗?如何使用 getCurrentPosition 实现 getCurrentCity?

【问题讨论】:

    标签: javascript angularjs cordova


    【解决方案1】:

    如果我在这里回答了错误的问题,请原谅我,但如果你只是问如何从服务中返回多个方法,那很简单:

    angular.module('app', []).factory('geolocation', function ($rootScope, cordovaReady) {
        var locationObj = {
            getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {
    
                navigator.geolocation.getCurrentPosition(function () {
                    var that = this,
                        args = arguments;
    
                    if (onSuccess) {
                        $rootScope.$apply(function () {
                            onSuccess.apply(that, args);
                        });
                    }
                }, function () {
                    var that = this,
                        args = arguments;
                    if (onError) {
                        $rootScope.$apply(function () {
                            onError.apply(that, args);
                        });
                    }
                }, options);
            }),
    
            getCurrentCity: function() {
                this.getCurrentPosition(function(position){
                    //determine city
                    var city = ...
                    //how to apply city to scope?
                });
            }
        }
    
        return locationObj;
    });
    

    如果您问的是特定于 phonegap 的问题,请忽略!

    【讨论】:

    • 不,这不是特定于 phonegap 的。我会试试你的建议。
    • hm 我在 getCurrentCity 方法中得到“getCurrentPosition 未定义”
    • 试试改成this.getCurrentPosition()
    • 我编辑了您的答案,使其对我有用,此外,如何将城市变量应用于范围,以便将其传递给前端?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多