Last active
December 22, 2016 08:56
-
-
Save dherges/2a59d4defe113182340fea11156beac3 to your computer and use it in GitHub Desktop.
angular2 provider aliases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, NgModule, Provider, ClassProvider, FactoryProvider, | |
TypeProvider, ValueProvider } from '@angular/core'; | |
import { MyThingDoer } from './my-thing-doer.service.ts'; | |
@NgModule({ | |
providers: [ | |
MyThingDoer, | |
{ provide: MyThingDoer, useClass: MyThingDoer }, | |
{ provide: MyThingDoer, useValue: new MyThingDoer() }, | |
{ provide: MyThingDoer, useFactory: () => new MyThingDoer() }, | |
{ provide: MyThingDoer, useFactory: function() { return new MyThingDoer(); } }, | |
] | |
}) | |
export class MyProvidersModule {} | |
// why is that all equivalent? let's rewrite the stuff even more explicit: | |
const typeProvider: TypeProvider | |
= MyThingDoer; | |
const valueProvider: ValueProvider | |
= { provide: MyThingDoer, useValue: new MyThingDoer() }; | |
const classProvider: ClassProvider | |
= { provide: MyThingDoer, useClass: MyThingDoer }; | |
const factoryProvider: FactoryProvider | |
= { provide: MyThingDoer, useFactory: function() { return new MyThingDoer(); } }; | |
const myProviders: Provider[] = [ | |
typeProvider, | |
valueProvider, | |
classProvider, | |
factoryProvider, | |
]; | |
@Component({ | |
providers: myProviders | |
}) | |
export class MyComponent {} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment