From 3d696936920cb8682dd47b007557363f17ef0df9 Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Wed, 20 Nov 2019 17:02:41 -0800 Subject: [PATCH] fix(ivy): remove TNodeType assertion from `directiveInject` instruction (#33948) The assertion that we have in the `directiveInject` instruction is too restrictive and we came across some pattern where it throws unnecessarily. This commit removes that assertion for now and more detailed investigation is needed to decide is we need to restrict the set of TNodeType again. This commit also adds a test which triggered the TNodeType.View to come up in the `directiveInject` instruction, so it might be useful to avoid regressions during further refactoring. PR Close #33948 --- packages/core/src/render3/instructions/di.ts | 3 -- packages/core/test/acceptance/di_spec.ts | 42 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/core/src/render3/instructions/di.ts b/packages/core/src/render3/instructions/di.ts index 28ee4880e7..71a2f8b6d9 100644 --- a/packages/core/src/render3/instructions/di.ts +++ b/packages/core/src/render3/instructions/di.ts @@ -46,9 +46,6 @@ export function ɵɵdirectiveInject( // if inject utilities are used before bootstrapping. if (lView == null) return ɵɵinject(token, flags); const tNode = getPreviousOrParentTNode(); - ngDevMode && assertNodeOfPossibleTypes( - tNode, TNodeType.Container, TNodeType.Element, TNodeType.ElementContainer, - TNodeType.IcuContainer); return getOrCreateInjectable( tNode as TDirectiveHostNode, lView, resolveForwardRef(token), flags); } diff --git a/packages/core/test/acceptance/di_spec.ts b/packages/core/test/acceptance/di_spec.ts index 571c5dd2bb..863c3e3fcd 100644 --- a/packages/core/test/acceptance/di_spec.ts +++ b/packages/core/test/acceptance/di_spec.ts @@ -1859,4 +1859,46 @@ describe('di', () => { expect(fixture.nativeElement.innerHTML).toContain('Other value is: 2 (transformed)'); }); + + it('should support dependencies in Pipes used inside i18n blocks', () => { + @Injectable() + class MyService { + transform(value: string): string { return `${value} (transformed)`; } + } + + @Pipe({name: 'somePipe'}) + class MyPipe { + constructor(private service: MyService) {} + transform(value: any): any { return this.service.transform(value); } + } + + @Component({ + template: ` + + {{count | somePipe}} items + + + ` + }) + class MyComp { + count = '2'; + + @ViewChild('target', {read: ViewContainerRef}) target !: ViewContainerRef; + @ViewChild('source', {read: TemplateRef}) source !: TemplateRef; + + create() { this.target.createEmbeddedView(this.source); } + } + + TestBed.configureTestingModule({ + declarations: [MyPipe, MyComp], + providers: [MyService], + }); + const fixture = TestBed.createComponent(MyComp); + fixture.detectChanges(); + + fixture.componentInstance.create(); + fixture.detectChanges(); + + expect(fixture.nativeElement.textContent.trim()).toBe('2 (transformed) items'); + }); });