ES6 classes with Angular 1 DI issue with $inject

176 views Asked by At
class Foo {
    constructor(dep1) {
    }
}
Foo.$inject = ['Dependency1']

I know you can do DI like this in angular /w es6 classes. However, I need to also pass something into my constructor for foo (a config object). This is eluding me because from my knowledge, the DI parameters go first.

Essentially, I want to do

let foo = new Foo(config); //config defined above

But still have the benefits of the injectable dependencies in the class.

1

There are 1 answers

2
Conqueror On

Solution 1:

let angular = window.angular;
class Foo{
    constructor(config){
        let injector = angular.injector();
        let dep1 = injector.get('Dependency1');
        this.prop1 = config.prop;
        this.prop2 = dep1.get(); 
    }
}

Solution 2:

//assuming ES6 modules, export dependency as module
import dependency from 'path';
class Foo{
    constructor(config){
        this.prop1 = config.prop;
        this.prop2 = dependency.get();
    }
}