Angular工厂保持返回undefined

时间:2016-07-31 08:35:04

标签: angularjs ionic-framework factory

我的Angular工厂保持未定义的返回,下面是我正在尝试的两个片段。

这是我的 geoLocationService.js

myApp.factory('geoLocationService', function () {

return {
    getMyGeolocation: function  () {

        var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0 };

        var x = navigator.geolocation.getCurrentPosition(function success(pos) {
            return pos.coords;
        }, function error(err) {
            console.log(err);
        }, options);
    }
}
});

这是我的 controller.js

myApp.controller('homeCtrl', function($scope, geoLocationService){

$scope.shareThePost = function(){
    geoLocationService.getMyGeolocation().then(function(data){
        console.log(data)
    });
}
});

3 个答案:

答案 0 :(得分:0)

因为工厂的getMyGeolocation()函数没有返回任何内容。你必须添加

return x;

来自功能。因为你的承诺是将数据返回工厂但你的工厂没有将数据返回给控制器。

答案 1 :(得分:0)

你没有回复承诺。每个.then()都依赖于返回的承诺。

添加:

return x;

getMyGeolocation函数的末尾。更好的是,由于您未在其他任何地方使用x,您可以立即返回:

return navigator.geolocation.getCurrentPosition(....)

编辑:看起来getCurrentPosition未返回承诺,因此您无法链接.then()。相反,你需要自己用承诺包装它来实现你想要的东西:

myApp.factory('geoLocationService', function ($q) {

return {
    getMyGeolocation: function  () {

        var deferred = $q.defer();

        var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0 };

        navigator.geolocation.getCurrentPosition(function success(pos) {
            deferred.resolve(pos.coords);
        }, function error(err) {
            console.log(err);
            deferred.reject(err);
        }, options);

        return deferred.promise;
    }
}
});

答案 2 :(得分:0)

Dim Word

Dim WordDoc

Set Word = CreateObject("Word.Application")

Word.Visible = False

Set WordDoc = Word.Documents.open("D:\working_folder\abc.doc")

Word.Run "<macroname>"

WordDoc.Save

Word.Quit


Set WordDoc = Nothing
Set Word = Nothing