adding resolver to angular routeProvider in config

112 views Asked by At

i start working with angular 1.x boilerplate and have 1 problem, i can`t send into resolver promise from service

function ExampleService($http) {
'ngInject';

const service = {};
let getTasks = ()=> {
    return $http.get('tasks');
};

service.isReady = new Promise((resolve, reject)=> {
    getTasks().then(
        (data) => {
            resolve(data);
        },
        (err, status) => {
            reject(err, status);
        });
});

return service;

}

export default { name: 'ExampleService', fn: ExampleService }; But when i try inject ,y service to on_config.js i have error

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:unpr] Unknown provider: ExampleService

My stateProvider

$stateProvider
    .state('Home', {
    url: '/',
    controller: 'ExampleCtrl as home',
    templateUrl: 'home.html',
    title: 'Home',
    resolve:{example:ExampleService.isReady}
    })

Maybe i do something wrong or you can suggest how can i impelemt logic for preloading some data to app before rendering first controller

P.S. link to bolerplate

1

There are 1 answers

1
georgeawg On BEST ANSWER

Services can't be injected into config blocks. They need to be injected in resolver functions:

$stateProvider
    .state('Home', {
    url: '/',
    controller: 'ExampleCtrl as home',
    templateUrl: 'home.html',
    title: 'Home',
    resolve:{
       example: function (ExampleService) {
           return ExampleService.isReady();
       }
    })

Also the isReady needs to be a function, not a new promise:

service.isReady = () => {
    return getTasks().then(response => response.data);
}