角6创建TemplateRef的实例

Ser*_*tov 5 javascript testing typescript angular6

我有(角度6)组件和TemplateRef<any>类型的输入参数

class TestComponent {
    @Input() tpl: TemplateRef<any>;
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在,我想创建一个测试,但找不到如何在测试中创建模板并将其设置为输入字段的方法。

在我的HTML中,我有类似的模板

<test>
    <ng-template #tpl>
        <div>OLOLO</div>
    </ng-template>
</test>
Run Code Online (Sandbox Code Playgroud)

我需要这样的东西

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.template = here I need instance of TemplateRef
Run Code Online (Sandbox Code Playgroud)

谢谢

Ked*_*444 1

我不明白你为什么这样做,但如果你想访问规范文件中的模板,而不是viewchild在组件中使用输入

所以使用代码将是

class TestComponent {
  @ViewChild('tpl') public tpl: ElementRef;
  ...
}
Run Code Online (Sandbox Code Playgroud)

在spec.ts中尝试这个

it('should create', () => {
  console.log(component.tpl);
  expect(component).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)