Using service / provider with $ http and $ q parameters in angular configuration

I want to load some configuration for each controller in the app.config section. Each controller needs a different, but not mutually exclusive, dataset to load. I can’t figure out how to achieve this.

.config(['$routeProvider', '$locationProvider', 
        function($routeProvider, $locationProvider){


    $routeProvider
    .when('/', {
        templateUrl: "partials/pages/dashboard.html",
        controller: "dashboard_controller",
        resolve: { dash_config: 'SomeConfigD'},
    })
    .when('/a', {
        templateUrl: "partials/pages/a.html",
        controller: "a_controller",
        resolve: { dash_config: 'SomeConfigA'},
    })
}])

However, I do not want to write separate factories for someConfigAand someConfigD, since they share code. I want something like

app.factory('configFactory', function(...){
    var factory = ;

    function get1(){
        // some $http calls here and return a promise
    }

    function get2(){
        // some $http calls here and return a promise
    }

    function get3(){
        // some $http calls here and return a promise
    }
    factory.configA = function(){
        // return a promise to resolve both get1 and get2
    };
    factory.configD = function(){
        // return a promise to resolve both get2 and get3
    };
})

How can i do this?

+3
source share
1 answer

It looks like you're looking $q.allfor that you can read about here .

fiddle, factory, , factory. :

f.configA = function(){
    var get12 = $q.all([
        get1().then(thenFn),
        get2().then(thenFn)
    ]).then(function() {
        console.log('both resolved');
    });
    return get12;
};

, promises ( , $timeout

get , , !

0

All Articles