Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 6 create instance of TemplateRef

I have (angular 6) component and input parameter of TemplateRef<any> type

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

<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

like image 830
Sergey Bakhmatov Avatar asked Nov 08 '22 03:11

Sergey Bakhmatov


1 Answers

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

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();
});
like image 91
Kedar9444 Avatar answered Nov 15 '22 05:11

Kedar9444