简体   繁体   English

angular 6创建TemplateRef的实例

[英]angular 6 create instance of TemplateRef

I have (angular 6) component and input parameter of TemplateRef<any> type 我有(angular 6)组件和TemplateRef<any>类型的输入参数

class TestComponent {
    @Input() tpl: TemplateRef<any>;
    ...
}

Now I want to create a test and I can't find the way how to create a template in test and set it as an input field. 现在我想创建一个测试,我找不到如何在测试中创建模板并将其设置为输入字段的方法。

In my HTML I have template like 在我的HTML中我有像这样的模板

<test>
    <ng-template #tpl>
        <div>OLOLO</div>
    </ng-template>
</test>

I need something like this 我需要这样的东西

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.template = here I need instance of TemplateRef

Thanks 谢谢

I didn't got why you are doing it this way, but if you want to access the template in spec file, instead of using input use viewchild in the component 我不知道你为什么这样做,但如果你想访问spec文件中的模板,而不是使用输入使用viewchild中的viewchild

so use code will be 所以使用代码将是

class TestComponent {
  @ViewChild('tpl') public tpl: ElementRef;
  ...
}

and in spec.ts try this 并在规格中试试这个

it('should create', () => {
  console.log(component.tpl);
  expect(component).toBeTruthy();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM