[英]how to call factory function from another factory in the same module in angularJS
[英]How to call a factory from a factory in AngularJS
我無法對此找到一個明確的答案,因此請您提前道歉。
(此工具) 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來確保兩者都能解決,但目前我似乎沒有查詢工廠的正確方法,所以我的問題是如何從另一個工廠查詢工廠?
您可以使用$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;
}
};
看到這個矮人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.