我有 TemplateRef<any>
的( Angular 6)组件和输入参数类型
class TestComponent {
@Input() tpl: TemplateRef<any>;
...
}
现在我想创建一个测试,但我找不到如何在测试中创建模板并将其设置为输入字段的方法。
在我的 HTML 中我有这样的模板
<test>
<ng-template #tpl>
<div>OLOLO</div>
</ng-template>
</test>
我需要这样的东西
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.template = here I need instance of TemplateRef
谢谢
最佳答案
我不明白你为什么要这样做,但如果你想访问规范文件中的模板,而不是使用输入,请在组件中使用 viewchild
所以使用代码会是
class TestComponent {
@ViewChild('tpl') public tpl: ElementRef;
...
}
在 spec.ts 中试试这个
it('should create', () => {
console.log(component.tpl);
expect(component).toBeTruthy();
});
关于javascript - Angular 6 创建 TemplateRef 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655489/