Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You're providing $inject array inside the function itself... It's not the proper way to do things. It should be provided before function is called

function layerQueryer($http,$q,overlayLayersConf){
 ....
}
layerQueryer.$inject = ['$http','$q','overlayLayersConf'];

after the three promises are fulfilled I no longer have a reference to the overlayLayersConf service passed into the service constructor

How do you find that out? Any of the code presented doesn't use overlayLayersConf. It means that it won't be saved into closure so in debugger overlayLayersConf will be undefined inside all sub-calls.

joinArrays in first snippet is a private function. It can be accessed only from the code inside the service only

share|improve this answer
    
I was using the debugger - you were right without a lot to go on so thanks. I hadn't appreciated that the unused code wasn't inserted into closure callbacks but it makes sense. Thanks. Have also put the $inject just before the function - that's what the style guide recommends and is more readable so hopefully that won't cause me any more problems. – Stev_k Aug 18 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.