javascript - How to handle closures in TypeScript (Angular injections)? -


i have angular factory service in javascript defined this:

app.service('myservicefactory', ['$http', '$timeout', '$interval',   function($http, $timeout, $interval) {    function myservice() {     // can use injected values here without additional efforts   }    this.create = function() {     return new myservice();   }  }]); 

now want convert typescript:

module services {    export class myservicefactory {     static $inject: string[] = ['$timeout', '$interval', '$http'];      constructor(       private timeout: angular.itimeoutservice,       private interval: angular.iintervalservice,       private http: angular.ihttpservice) {     }     public create(): myservice { return new myservice(); };   }    export class myservice() {     // have problem here. need redefine ,     // initialize variables, injected factory class   }    angular.module('mymodule').service('myservicefactory', myservicefactory); } 

do see mean? typescript not allow nested classes, have solved issue. typescript solution looks uncool. there more elegant solution?

instead of :

export class myservice() {     // have problem here. need redefine ,     // initialize variables, injected factory class   } 

you can put create on myservicefactory i.e.:

module services {    export class myservicefactory {     static $inject: string[] = ['$timeout', '$interval', '$http'];      constructor(       private timeout: angular.itimeoutservice,       private interval: angular.iintervalservice,       private http: angular.ihttpservice) {     }     public create(){        // use revealing module pattern        // , let compiler infer return type       // e.g.       var foo = 23;       return {          foo       }     };   }    angular.module('mymodule').service('myservicefactory', myservicefactory); } 

please note valid javascript valid typescript (more)


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -