Search code examples
angularjstypescriptdependency-injectionangularjs-serviceangularjs-factory

Inject Angular factory dependency into Typescript class without polluting constructor?


I'm writing an factory as a Typescript class and instantiating it with new FactoryName() when needed.

I'm getting it working as a factory by doing this: .factory('FactoryName', function() { return FactoryName; }) and it works great, but now I need to inject another factory I've made outside of Typescript into it without polluting the constructor call. Is that possible?


Solution

  • I found a workaround:

    export function FactoryName(injectedService1, injectedService2) {
      return class FactoryClass {
        constructor() {
          let test = injectedService1.testFunction();
        }
      }
    }
    
    app.factory('FactoryName', FactoryName)