1

I had build a Angularjs provider in typescript wondering is there any better way of doing the same,my sample provider create an abstraction of console logger,the interface is just to keep the compiler happy , and I register the provider as in the code below

module ec{
    export interface ILoggerService{
    warn(message:string):void;
    info(message:string):void;
    error(message:string):void;
    debug(message:string):void;
    log(message:string,exception?:any):void;

   }
}

module ec.providers {

    export class loggerService {
      private loggerState:boolean = false;

    public start =() =>{
        this.loggerState = true;
    }

    public $get =['$log',($log:ng.ILogService) =>{
        return {
            warn:(message) =>{
                  if(this.loggerState)
                  $log.warn(message);
            },
            info:(message) =>{
                if(this.loggerState)
                    $log.info(message);
            },
            error:(message) =>{
                if(this.loggerState)
                    $log.error(message);
            },
            debug:(message) =>{
                if(this.loggerState)
                    $log.debug(message);
            },
            log:(message,exception) =>{
                if(this.loggerState)
                    $log.log(message,exception);
            }

        }
    }]

   }

}


module ec{

  var app =angular.module('foo',[]).config(['loggerServiceProvider',  (loggerServiceProvider)=>{
              if(location.hostname =='localhost') {
                      loggerProvider.start();
               }
      }]);
      app.provider(providers);
}
2
  • When is called the method start? Commented Apr 13, 2015 at 13:15
  • @Tarh added the start method use. Commented Apr 14, 2015 at 5:39

1 Answer 1

2

With a provider: The dynamic way

module MyNameSpace {
    export interface ILoggerService {
        warn(message: string): void;
        info(message: string): void;
        error(message: string): void;
        debug(message: string): void;
        log(message: string, exception?: any): void;
    }
    export function loggerService {
        var serv = new LoggerService();
        this.start = () => {
            // ...
        };
        this.$get = ['$log',($log: ng.ILogService) => {
            var methods = ['warn', 'info', 'error', 'debug', 'log'],
                obj: any = {};
            for (var i = 0; i < methods.length; ++i)
                obj[methods[i]] = (msg: string) => $log[methods[i]](msg);
            return obj;
        }];

    }
}

With a provider: Class

module MyNameSpace {
    export interface ILoggerService {
        warn(message: string): void;
        info(message: string): void;
        error(message: string): void;
        debug(message: string): void;
        log(message: string, exception?: any): void;
    }
    export function loggerService {
        var serv = new LoggerService();
        this.start = () => {
            // ...
        };
        this.$get = ['$log',($log:ng.ILogService) => {
            serv.start($log);
            return serv;
        }];

    }
    class LoggerService implements ILoggerService {
        private $log: ng.ILogService;
        public start($log: ng.ILogService) {
            this.$log = $log;
        }
        public warn(message) {
            if(this.$log)
                this.$log.warn(message);
        }
        // ...
    }
}

With a service

You can replace the provider with a service:

class LoggerService {
    private loggerState = false;
    static $inject = ['$log'];
    constructor(private $log: ng.ILogService) {
    }
    public start() {
        this.loggerState = true;
    }
    public warn(message) {
        if(this.loggerState)
            this.$log.warn(message);
    }
    // ...
}

app.service('LoggerService', LoggerService);
Sign up to request clarification or add additional context in comments.

1 Comment

i need the provider to do some initial configuration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.