Last active
April 27, 2017 07:05
-
-
Save deebloo/8b4dc70e8e3ca53d8dba3b8978a343cf to your computer and use it in GitHub Desktop.
Angular2 Service Pattern for configurability and reusability
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
// Standard Service Class | |
export class MyService { | |
constructor(private config) { } | |
} | |
// factory for creating new instances of the service | |
function createMyService(config = {}) { | |
return new MyService(config); | |
} | |
// service provider to provide configuration for service when provided through DI | |
// THis is really the only angulary bit here | |
function provideMyService(config) { | |
return [ | |
{ | |
provide: MyService, | |
useFactory() { | |
return createMyService(config); | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment