Was the angular2 dependency injection container designed for standalone use, and is it possible to use it for typescript/javascript server-side applications ?
I opened an issue on Oct. 16 (https://github.com/angular/di.js/issues/108) on the di project which was I guess supposed to make it into v2. Apparently this was not possible. What's up with the new angular2 di ? Can I use it in a standalone fashion with js / es6 / ts projects?
Dependency injection, or DI, is a design pattern in which a class requests dependencies from external sources rather than creating them. Angular's DI framework provides dependencies to a class upon instantiation. Use Angular DI to increase flexibility and modularity in your applications.
TypeScript is available as a package on the npm registry available as "typescript" . You will need a copy of Node. js as an environment to run the package.
The "Application Module" can be injected as a dependency in AngularJS.
You do need Node. js to develop Angular applications. All the tools you will run, while developing it, uses Node. js to run, like npm and the Angular CLI itself.
Seems someone has extracted dependency injection from Angular2 recently(Jan 2017), which allow it to use outside of the framework.
https://github.com/mgechev/injection-js
As of Angular 2 RC.5, DI is a part of @angular/core
package. For non-Angular uses, it was recently extracted into injection-js
package by Minko Gechev, a member of Angular team.
Here is a short ES6, Node-friendly example:
// may not be needed for injection-js and recent @angular/core versions // if ES5-flavoured `Class` helper isn't used require('reflect-metadata'); const { Inject, Injector, ReflectiveInjector, OpaqueToken } = require('@angular/core'); // or ... = require('injection-js'); const bread = new OpaqueToken; const cutlet = new OpaqueToken; class Sandwich { constructor(bread, cutlet, injector) { const anotherBread = injector.get('bread'); injector === rootInjector; bread === 'bread'; anotherBread === 'bread'; cutlet === 'cutlet'; } } Sandwich.parameters = [ new Inject(bread), new Inject(cutlet), new Inject(Injector) ]; const rootInjector = ReflectiveInjector.resolveAndCreate([ { provide: 'bread', useValue: 'bread' }, { provide: bread, useValue: 'bread' }, { provide: cutlet, useValue: 'cutlet' }, Sandwich ]); const sandwich = rootInjector.get(Sandwich);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With