Skip to content
/ ts-di Public
forked from angular/di.js

TypeScript version of Angular Dependency Injection Framework

License

Notifications You must be signed in to change notification settings

AsterAI/ts-di

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Dependency Injection

This fork - it's just version TypeScript Angular di.js.

Install

npm install ts-di --save

Examples resolve dependency

Inject instance of class A for constructor of class B:

Using decorator

import {Inject} from 'ts-di';

class A{}

// All dependencies in @Inject listed separated by commas
@Inject(A)
class B
{
  constructor(private a: A){}

  getValue()
  {
    console.log(`There should be a instance of class A:`, this.a);
  }
}

Using annotation a function

import {annotate, InjectDecorator} from 'ts-di';

class A{}

class B
{
  constructor(private a: A){}

  getValue()
  {
    console.log(`There should be a instance of class A:`, this.a);
  }
}

// All dependencies in InjectDecorator listed separated by commas
annotate( B, new InjectDecorator(A) );

Get instance

import {Injector} from 'ts-di';
import {B} from './path/to/class/B';

let injector = new Injector();

// Instance class B and resolve dependency
let instance = injector.get(B);

Usage decorators

Now supports five decorators:

  • For resolving chain dependencies:
    • @Inject
    • @InjectPromise
    • @InjectLazy
  • For resolving single dependency (useful for testing):
    • @Provide
    • @ProvidePromise

About

TypeScript version of Angular Dependency Injection Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%