javascript - ES6 classes with Angular 1 DI issue with $inject -
class foo { constructor(dep1) { } } foo.$inject = ['dependency1']
i know can di in angular /w es6 classes. however, need pass constructor foo (a config object). eluding me because knowledge, di parameters go first.
essentially, want do
let foo = new foo(config); //config defined above
but still have benefits of injectable dependencies in class.
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 module import dependency 'path'; class foo{ constructor(config){ this.prop1 = config.prop; this.prop2 = dependency.get(); } }
Comments
Post a Comment