Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the service provider when registering using typescript class

The following service provider registers successfully. Other controllers can use the service without trouble. My question is how to access the provider in order to configure it before the service is instantiated by the $injector service?

module apiService{

    export interface IBaseService{
        leagueID: number;
    }

    export interface IApiBaseServiceProvider extends ng.IServiceProvider{
        setLeagueID(leagueID: number): void;
        $get(): IBaseService;
    }

    class BaseServiceProvider implements IApiBaseServiceProvider{

        private _leagueID: number;

        $get(){
            return new BaseService(this._leagueID);
        }

        setLeagueID(leagueID: number){
            this._leagueID = leagueID;
        };


    }

    class BaseService implements IBaseService{

        constructor(private _leagueID: number){};

        get leagueID(){
            return this._leagueID;
        }

    }

    angular.module('apiService').provider('apiService.baseService', BaseServiceProvider);

}

I've tried using the class name as the accessor but the entire application fails when doing so.

angular.module('apiService').config(['BaseServiceProvider', function(baseServiceProvider: IApiBaseServiceProvider){
    baseServiceProvider.setLeagueID(12);    
}]);

The documentation only shows an example where a named function is used to instantiate the provider. Later in the explanation, the named function's name is used to access the provider.

Because a TypeScript class is used in the above example there is no named function to use to access the provider. So, how does one configure the provider when using a TypeScript class?

like image 610
proff321 Avatar asked Jun 12 '15 21:06

proff321


People also ask

What is service provider in Angular?

A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time, these dependencies are services that you create and provide. For the final sample application using the provider that this page describes, see the live example / download example .

What are providers in Angular module?

Providers are classes that create and manage service objects the first time that Angular needs to resolve a dependency. Providers is used to register the classes to an angular module as a service. And then, this service classes can be used by other components during the itself creation phase in the module.


1 Answers

Problem is with your registration of provider. Instead of

  angular.module('apiService').provider('apiService.baseService', BaseServiceProvider);

do:

 angular.module('apiService').provider('BaseService', BaseServiceProvider);

and access in the config phase like you do, BaseServiceProvider (postfix word Provider). When injecting the provider instance anywhere else you could just use BaseService. So basically you don't inject with the class name, instead you inject with the name it is registered with.

like image 67
PSL Avatar answered Oct 02 '22 04:10

PSL