我无法找到明确的答案,如果有的话,请提前道歉。
(此plunker)http://plnkr.co/edit/EmEQt9KlApc9I1AIraBZ?p=catalogue显示我的代码的当前状态。有一个控制器返回一个http查询:
(function() {
'use strict';
angular
.module('marketsApp')
.controller('marketLocationController', marketLocationController);
marketLocationController.$inject = ['$scope', '$routeParams', 'marketLocationService'];
function marketLocationController($scope, $routeParams, marketLocationService) {
$scope.center = {};
$scope.markers = {};
// console.log(geo.coords);
activate ();
function activate () {
getMarketLocations();
}
function getMarketLocations() {
var marketList = marketLocationService.getMarkets();
$scope.marketList = marketList;
// insert list template here
}
}
})();
查询需要三个参数,即该工厂的位置:
(function() {
'use strict';
angular
.module('marketsApp')
.factory('BrowserLocation', BrowserLocation);
BrowserLocation.$inject = ['$q'];
function BrowserLocation() {
return {
getLocation: function () {
var deferred = $q.defer();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
deferred.resolve(position);
});
}
return deferred.promise;
}
};
}
})();
来自这个工厂的两个参数,它们可以是查询,也可以从本地存储访问:
(function() {
'use strict';
angular
.module('marketsApp')
.factory('userDefaultsService', userDefaultsService);
userDefaultsService.$inject = ['$q'];
function userDefaultsService($q) {
return {
getDefaults: function () {
var deferred = $q.defer();
var userDefaults = {
dist: 25,
limit: 100
};
return deferred.promise;
}
};
}
})();
并在此工厂中构建查询:
(function() {
'use strict';
angular
.module('marketsApp')
.factory('marketLocationService', marketLocationService);
marketLocationService.$inject = ['$http', '$q', 'BrowserLocation', 'userDefaultsService'];
function marketLocationService($http, $q, BrowserLocation, userDefaultsService) {
var apiUrl = "/api/markets/bylocation";
return {
getMarkets: getMarkets
};
function getMarkets () {
// var self = this;
var apiParams = {
point: null,
dist: null,
limit: null
};
var thisLocation = null;
var thisDefault = null;
thisLocation = function (location) {
BrowserLocation.getLocation()
};
thisDefault = function (defaults) {
userDefaultsService.getDefaults();
};
// extract the params here
// (this is how it should work)
// lat = thisLocation.coords.latitude;
// lon = thisLocation.coords.longitude;
// apiParams.point = lat + "," lon;
// apiParams.dist = thisDefault.dist;
// apiParams.limit = thisDefault.limit;
$http({
url: apiUrl,
method: 'GET',
params: {
point: point,
dist: dist,
limit: limit
}
});
}
}
})();
我无法获得BrowserLocation和userDefaultsService来返回其对象,以便我可以将它们传递给apiParams。我知道我必须使用$ q或.then确保两者都有解决方法,但目前我似乎没有正确的查询工厂的方法,所以我的问题是我如何从另一个工厂查询工厂工厂?
答案 0 :(得分:0)
您可以使用$q.all
等待多个承诺来解决。在marketLocationService
中等待BrowserLocation.getLocation
和userDefaultsService.getDefaults
。然后当他们解决时执行$http
请求:
$q.all([BrowserLocation.getLocation(), userDefaultsService.getDefaults()]).then(function(values) {
// here values[0] is response from getLocation
// values[1] is response from getDefaults
$http({
url: apiUrl,
method: 'GET',
params: {
point: point,
dist: dist,
limit: limit
}
});
});
您还需要解决userDefaultsService.getDefaults
,假设它应该是异步的:
return {
getDefaults: function () {
var deferred = $q.defer();
var userDefaults = {
dist: 25,
limit: 100
};
$timeout(function() {
deferred.resolve(userDefaults);
})
return deferred.promise;
}
};
请参阅this plunker