在我的控制器中使用我的工厂时出现 'undefined' 错误

Getting an 'undefined' error when working with my factory in my controller

大家好,我正在开发一款新应用,但 运行 遇到了障碍。我的工厂本身运行良好,但是当我尝试在我的控制器中使用它时,我得到:

Error: getPlaces.getPlaces(...) is undefined

这里是我工厂的代码:

angular.module('Space')
.factory('getPlaces', ['$http', function($http) {
    var placesFactory = {};

    placesFactory.getPlaces = function() {


        if (navigator.geolocation) {
            var geoLoc = navigator.geolocation;
            return geoLoc.getCurrentPosition(function(position) {
                var lat = position.coords.latitude,
                    lng = position.coords.longitude;

            return getFSinfo(lat, lng);
        });

        } else {
            alert('Opps. Looks like you have your location turned off. Space uses your location to find places near you.');
        }


        function getFSinfo (lat, lng) {
            var clientID = 'J2HCJIGLTSFFJMKMLAEBT30QNLINBSDQBBDZNRRH1MUSDZ4V',
                clientSercet = 'RJBO0O01D2OSPR5R2KF4S214CFNXAOAD03DJM3L15ERLUCXF',
                coords = lat + ',' + lng,
                searchIds = '';

                var categoryIds = {
                    movie: '4bf58dd8d48988d17f941735',
                    museum: '4bf58dd8d48988d181941735',
                    stadium: '4bf58dd8d48988d184941735',
                    themePark: '4bf58dd8d48988d182941735',
                    waterPark: '4bf58dd8d48988d193941735',
                    eventVenue: '4d4b7105d754a06373d81259',
                    food: '4d4b7105d754a06374d81259',
                    mall: '4bf58dd8d48988d1fd941735',
                    musicStore: '4bf58dd8d48988d1fe941735',
                    foodDrink: '4bf58dd8d48988d1f9941735',
                    clothesStore: '4bf58dd8d48988d103951735'

                };


                for (var key in categoryIds) {
                    searchIds += categoryIds[key] + ',';
                }

                var FSUrl = 'https://api.foursquare.com/v2/venues/search?ll=' + coords + '&client_id=' + clientID + '&client_secret=' + 
                clientSercet +'&intent=browse&radius=20000' + '&categoryId=' + searchIds + '&v=20130815' + '&limit=50';


                return $http.get(FSUrl)
                    .then(function(response) {

                        var data = response.data.response;
                        console.log(data);
                        return data;

                    }, function(error) {
                        console.log(error);
                    }); 
        }



};



        return placesFactory;
}]);

控制器:

angular.module('Space')
.controller('MainCtrl', ['getPlaces', '$http', function(getPlaces, $http) {
    var vm = this;
    vm.message = 'Hello world!';

    getPlaces.getPlaces().then(function(data) {
        vm.place = data;
        console.log(vm.place);
    });

}]);

我不明白为什么这么想。

任何帮助都会很棒!

您的控制器期望 getPlaces() 到 return 一个承诺。 getCurrentPosition() 没有 return 任何东西。您可以像这样创建自己的承诺:

if (navigator.geolocation) {
    var geoLoc = navigator.geolocation;
    return $q(function(resolve) {
        geoLoc.getCurrentPosition(function(position) {
            var lat = position.coords.latitude,
                lng = position.coords.longitude;

            resolve(getFSinfo(lat, lng));
        });
    });
}

务必将 $q 注入工厂。最后,要像我一样使用 $q,您需要 Angular 1.3+。如果您使用的是早期版本,您可以使用 deferreds.

完成同样的事情