I have a custom service, which has a dependency on another service and has an external api and some internal functions.
angular.module('DashboardApp').factory('layerQueryer', layerQueryer);
function layerQueryer($http, $q, overlayLayersConf) {
layerQueryer.$inject = ['$http', '$q', 'overlayLayersConf'];
function joinArrays(array1, array2, keya, keyb) {
.....
};
function getLayerList() {
var def = $q.defer();
var Promise1 = dosomething();
var Promise2 = dosomethingelse();
var Promise3 = doanotherthing();
$q.all([Promise1, Promise2, Promise3])
.then(function (data) {
def.resolve('xxxx');
})
.
catch (function (error) {
console.log(error);
})
return def.promise;
};
return {
getLayerList: getLayerList,
};
}
I have two problems with this service - one when the callback executes after the three promises are fulfilled I no longer have a reference to the overlayLayersConf service passed into the service constructor, which I need, and I also do not have a reference to joinArrays when I need to use it around the same time.
I have found that if I turn the JoinArrays function into a variable and make an additional two functions and expose them:
function getJoinArray(){
return joinArrays;
}
// for some reason this seems to allow the scope to be kept during the promise callback...
function getOverlayLayers(){
return overlayLayersConf;
}
return {
getJoinArray:getJoinArray,
getLayerList: getLayerList,
getOverlayLayers: getOverlayLayers,
};
Then I can get references to these variables. This seems intuitive but strange at the same time. Can anyone tell me what is going on here?