fix(core): allow module providers to overwrite providers from `ModuleWithProviders`

Fixes #10313
Closes #10317
This commit is contained in:
Tobias Bosch 2016-07-27 01:52:31 -07:00
parent 367f0fd142
commit 553344739c
2 changed files with 32 additions and 12 deletions

View File

@ -210,18 +210,6 @@ export class CompileMetadataResolver {
const entryComponents: cpl.CompileTypeMetadata[] = [];
const schemas: SchemaMetadata[] = [];
if (meta.providers) {
providers.push(...this.getProvidersMetadata(meta.providers, entryComponents));
}
if (meta.entryComponents) {
entryComponents.push(
...flattenArray(meta.entryComponents)
.map(type => this.getTypeMetadata(type, staticTypeModuleUrl(type))));
}
if (meta.schemas) {
schemas.push(...flattenArray(meta.schemas));
}
if (meta.imports) {
flattenArray(meta.imports).forEach((importedType) => {
let importedModuleType: Type;
@ -295,6 +283,20 @@ export class CompileMetadataResolver {
});
}
// The providers of the module have to go last
// so that they overwrite any other provider we already added.
if (meta.providers) {
providers.push(...this.getProvidersMetadata(meta.providers, entryComponents));
}
if (meta.entryComponents) {
entryComponents.push(
...flattenArray(meta.entryComponents)
.map(type => this.getTypeMetadata(type, staticTypeModuleUrl(type))));
}
if (meta.schemas) {
schemas.push(...flattenArray(meta.schemas));
}
transitiveModule.entryComponents.push(...entryComponents);
transitiveModule.providers.push(...providers);

View File

@ -960,6 +960,24 @@ function declareTests({useJit}: {useJit: boolean}) {
expect(injector.get('token1')).toBe('direct');
});
it('should overwrite the providers of imported ModuleWithProviders', () => {
@NgModule()
class ImportedModule {
}
@NgModule({
providers: [{provide: 'token1', useValue: 'direct'}],
imports: [
{ngModule: ImportedModule, providers: [{provide: 'token1', useValue: 'imported'}]}
]
})
class SomeModule {
}
const injector = createModule(SomeModule).injector;
expect(injector.get('token1')).toBe('direct');
});
it('should overwrite the providers of imported modules on the second import level', () => {
@NgModule({providers: [{provide: 'token1', useValue: 'imported'}]})
class ImportedModuleLevel2 {