Skip to content

Instantly share code, notes, and snippets.

@mildronize
Last active November 6, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildronize/8b52a7acbbf0c30c17fa6da917dee49b to your computer and use it in GitHub Desktop.
Save mildronize/8b52a7acbbf0c30c17fa6da917dee49b to your computer and use it in GitHub Desktop.
Example usage of ReflectiveInjector in `injection-js` ( Angular 4 API) read more: https://v4.angular.io/api/core/ReflectiveInjector ( @Injectable should separate file)
import 'reflect-metadata';
import { ReflectiveInjector, Injectable, Injector } from 'injection-js';
class Service {
get() {
return "my service";
}
}
class Service2 {
get() {
return "my service 2";
}
}
@Injectable()
class Controller {
constructor(public service: Service, public service2: Service2) {}
}
const injector: Injector = ReflectiveInjector.resolveAndCreate([Service, Controller, Service2]);
const controller = injector.get(Controller) as Controller;
console.log(controller.service.get());
console.log(controller.service2.get());
/**
* Output
* my service
* my service 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment