-
-
Save sethbergman/3adedb5aa3e0a5e47e12f810e20ad7c9 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