How to manually annotate an imported class (in angular2)?

23 views
Skip to first unread message

Andreas Grimm

unread,
Nov 23, 2015, 3:56:23 PM11/23/15
to AngularJS
I have some dependency-free business logic (ES6) classes that I want the Injector to make instances off of.

After having these classes imported (via ES6 import statement), how can I make them "injectable"?
I'm not able to decorate them with `@Injectable()` because these classes must stay dependency-free POJOs.

Here's an example:

```
import { Car, Engine } from 'my-externally-loaded-package';

require('reflect-metadata');  // runs on node.js
let ng = require('angular2/core');  // runs on node.js

// here I'd need to tell angular2-DI that Car needs deps injected

let providers = [
  ng.provide(Engine, { useClass: Engine }),
  ng.provide(Car, { useClass: Car })
];

let injector = ng.Injector.resolveAndCreate(providers);
let car = injector.get(Car);

```

If I decorate the Car class accordingly then of course everything works fine. But the mentioned classes must stay dependency-free and not have a dependency to a specific framework like angular.


thanks for any help on this :)
 Andreas



Andreas Grimm

unread,
Nov 23, 2015, 6:28:42 PM11/23/15
to AngularJS
with dependency-free I meant no deps to 3rd-Party libs or frameworks. the Car class actually has a dep to the Engine class in its constructor.

class Car {
  engine: Engine;
  
  constructor(engine: Engine) {
    this.engine = engine;
  }

Pascal Precht

unread,
Nov 24, 2015, 4:35:35 PM11/24/15
to AngularJS
This is all being discussed here https://github.com/angular/angular/issues/4404

Unfortunately, there's currently no way to get around this unless TS comes up with a dedicated API :(
Reply all
Reply to author
Forward
0 new messages