Skip to content

Instantly share code, notes, and snippets.

@deebloo
Last active April 27, 2017 07:05
Show Gist options
  • Save deebloo/8b4dc70e8e3ca53d8dba3b8978a343cf to your computer and use it in GitHub Desktop.
Save deebloo/8b4dc70e8e3ca53d8dba3b8978a343cf to your computer and use it in GitHub Desktop.
Angular2 Service Pattern for configurability and reusability
// 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