Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 DI in Typescript. Can we use it in node.js / non-angular projects?

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?

like image 986
Ludovic C Avatar asked Dec 24 '15 14:12

Ludovic C


People also ask

What is DI in Angular and how it works?

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.

Is node required for TypeScript?

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.

Which components can be injected as a dependency in Angular JS?

The "Application Module" can be injected as a dependency in AngularJS.

Does Angular uses Nodejs?

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.


2 Answers

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

like image 97
Vincent Pang Avatar answered Sep 17 '22 16:09

Vincent Pang


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); 
like image 21
Estus Flask Avatar answered Sep 20 '22 16:09

Estus Flask