m0ngr31 m0ngr31 - 2 months ago 16
TypeScript Question

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?

Answer

I found a workaround:

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

app.factory('FactoryName', FactoryName)
Comments