m0ngr31 m0ngr31 - 1 year ago 100
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 Source

I found a workaround:

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

app.factory('FactoryName', FactoryName)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download