Created
June 5, 2018 18:24
-
-
Save alxhub/dd3fb7c09f347d6c21972b1088e1ae24 to your computer and use it in GitHub Desktop.
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
@Injectable({providedIn: ServiceModule}) | |
export class Service {} | |
@NgModule({}) export class ServiceModule {} | |
@NgModule({ | |
imports: [ServiceModule] | |
}) | |
export class AppModule {} | |
@Component({}) | |
export class Cmp { | |
constructor(svc: Service) {} | |
} | |
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
providers: [Service] | |
1) telling Angular which injector should create the service | |
2) actually registering the service with the injector | |
3) specifying when the service should be loaded (in terms of | |
lazy loading) |
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
export interface IService { | |
} | |
export const SERVICE_TOKEN = new InjectionToken<IService>({ | |
providedIn: 'root', | |
factory: () => inject(ServiceImpl) | |
}); | |
// useValue: foo | |
factory: () => foo, | |
// useExisting: SomeToken | |
factory: () => inject(SomeToken) | |
// useClass: SomeClass | |
factory: () => new SomeClass(inject(DepA), inject(DepB)), | |
// useFactory: factoryFn, deps: [DepA, DepB] | |
factory: () => factoryFn(inject(DepA), inject(DepB)) | |
//{provide: SERVICE_TOKEN, useClass: ServiceImpl} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment