Skip to content
Permalink
Browse files

fix(ivy): remove TNodeType assertion from `directiveInject` instructi…

…on (#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
  • Loading branch information
AndrewKushnir authored and matsko committed Nov 21, 2019
1 parent a172644 commit 90a9043abb0f0fd65ccc221965b95094a1bde065
Showing with 42 additions and 3 deletions.
  1. +0 −3 packages/core/src/render3/instructions/di.ts
  2. +42 −0 packages/core/test/acceptance/di_spec.ts
@@ -46,9 +46,6 @@ export function ɵɵdirectiveInject<T>(
// 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<T>(
tNode as TDirectiveHostNode, lView, resolveForwardRef(token), flags);
}
@@ -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: `
<ng-template #source i18n>
{{count | somePipe}} <span>items</span>
</ng-template>
<ng-container #target></ng-container>
`
})
class MyComp {
count = '2';

@ViewChild('target', {read: ViewContainerRef}) target !: ViewContainerRef;
@ViewChild('source', {read: TemplateRef}) source !: TemplateRef<any>;

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');
});
});

0 comments on commit 90a9043

Please sign in to comment.
You can’t perform that action at this time.