AngularJS无法识别Factory

时间:2014-12-11 02:59:44

标签: angularjs factory

我很难理解为什么控制器无法看到我的工厂。关于我做错了什么想法?

我的控制器:

myApp.controller('SNFController', ['$scope', '$http', '$routeParams', '$location', 'foundSnfs',
function($scope, $http, $routeParams, $location, foundSnfs) { 
    $scope.snfs = null; 
    $scope.chosensnf = []; 
    $scope.hidefilter = true; 
    $scope.getlocale = function() { 
        foundSnfs.getsnfs($scope.zip, $scope.miles).then(function(res){ 
        $scope.snfs = res; 
    })
}])

我的工厂:

myApp.factory('foundSnfs', function($http) { 
    var foundSnfs = {}; 
    foundSnfs.getsnfs = function (vz, vm){ 
        var g = new google.maps.Geocoder(); 
        g.geocode( { 'address': vz}, function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) { 
                var la = results[0].geometry.location.lat(); 
                var lo = results[0].geometry.location.lng(); 
                var cl = new google.maps.LatLng(la, lo); 
                var dm = vm * 1609.34; // distance in miles from meters 
                var url = 'http://data.medicare.gov/resource/hq9i-23gr.json?$where=within_circle(location,'+la+','+lo+','+dm+')&$ORDER=overall_rating DESC'; 
                return $http.get(url);
            }
        })
    }
});

我有2个输入字段,一个带有邮政编码,另一个带有里程范围。当用户点击按钮时,$ scope.locale将把zip和里程传递给工厂,工厂然后处理地理位置,然后从Medicare获得养老院列表。

谢谢。

2 个答案:

答案 0 :(得分:2)

AngularJs工厂需要一个返回值。 Factory

myApp.factory('foundSnfs', function($http) { 
var foundSnfs = {}; 
foundSnfs.getsnfs = function (vz, vm){ 
    var g = new google.maps.Geocoder(); 
    g.geocode( { 'address': vz}, function(results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
            var la = results[0].geometry.location.lat(); 
            var lo = results[0].geometry.location.lng(); 
            var cl = new google.maps.LatLng(la, lo); 
            var dm = vm * 1609.34; // distance in miles from meters 
            var url = 'http://data.medicare.gov/resource/hq9i-23gr.json?$where=within_circle(location,'+la+','+lo+','+dm+')&$ORDER=overall_rating DESC'; 
            return $http.get(url);
        }
    })
}
// factory needs to return
return foundSnfs; 
});

除此之外,如果g.geocode是异步的,您可能希望使用$q

答案 1 :(得分:1)

所以我看到的主要问题是g.geocode是异步的,所以你需要创建一个延迟对象并返回promise。这是一个例子......

myApp.factory('foundSnfs', function($q, $http) { 
    var foundSnfs = {}; 
    foundSnfs.getsnfs = function (vz, vm){
        var deferred = $q.defer(); 
        var g = new google.maps.Geocoder(); 
        g.geocode( { 'address': vz}, function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) { 
                var la = results[0].geometry.location.lat(); 
                var lo = results[0].geometry.location.lng(); 
                var cl = new google.maps.LatLng(la, lo); 
                var dm = vm * 1609.34; // distance in miles from meters 
                var url = 'http://data.medicare.gov/resource/hq9i-23gr.json?$where=within_circle(location,'+la+','+lo+','+dm+')&$ORDER=overall_rating DESC';

                // you can resolve a deferred object with another promise
                // which will resolve or reject appropriately.
                deferred.resolve($http.get(url));
            } else {
                deferred.reject(status);
            }
        });

        return deferred.promise;
    }

    return foundSnfs; // as eugene pointed out, you missed this
});

以下是如何使用进一步的承诺来解决或拒绝延迟对象的示例(例如$http

JSFiddle