简体   繁体   English

如何处理TypeScript中的闭包(角度注入)?

[英]How to handle closures in TypeScript (Angular injections)?

I have an Angular factory service in JavaScript that I defined like this: 我在JavaScript中定义了一个Angular工厂服务,如下所示:

app.service('MyServiceFactory', ['$http', '$timeout', '$interval',
  function($http, $timeout, $interval) {

  function MyService() {
    // I can use all injected values here without additional efforts
  }

  this.Create = function() {
    return new MyService();
  }

}]);

Now I want to convert it into TypeScript: 现在,我想将其转换为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() {
    // I have a problem here. I need to redefine and
    // initialize all variables, injected into my factory class
  }

  angular.module('MyModule').service('MyServiceFactory', MyServiceFactory);
}

Do you see what I mean? 你明白我的意思吗? TypeScript does not allow nested classes, which could have solved the issue. TypeScript不允许使用嵌套类,而嵌套类可以解决此问题。 Also TypeScript solution looks very uncool. 此外,TypeScript解决方案看起来也很酷。 Is there a more elegant solution? 有没有更优雅的解决方案?

Instead of : 代替 :

export class MyService() {
    // I have a problem here. I need to redefine and
    // initialize all variables, injected into my factory class
  }

You can put the Create on MyServiceFactory ie: 您可以将Create放在MyServiceFactory即:

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 the revealing module pattern 
      // And let the compiler infer the return type
      // e.g.
      var foo = 23;
      return {
         foo
      }
    };
  }

  angular.module('MyModule').service('MyServiceFactory', MyServiceFactory);
}

Please note that valid JavaScript is valid TypeScript ( more ) 请注意,有效的JavaScript是有效的TypeScript( 更多

You can pass the injected variables as parameters to your other class, eg: 您可以将注入的变量作为参数传递给其他类,例如:

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(this.timeout, this.interval, this.http);
  }
}

export class MyService {
  constructor(
    private timeout: angular.ITimeoutService,
    private interval: angular.IIntervalService,
    private http: angular.IHttpService) {
    // no more problems here, you can play with the injected variables again
  }
}

angular.module('MyModule').service('MyServiceFactory', MyServiceFactory);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM