Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to obtain a templateRef from a string?

I am interested how you can obtain a TemplateRef by passing a string for example.

I don't want to obtain it through a component html, as I just want to save it in a config to be used for all components. I am refering to the ng-bootstrap NgbDatePickerConfig that can be viewed here: https://github.com/ng-bootstrap/ng-bootstrap/blob/master/src/datepicker/datepicker-config.ts.

like image 892
apeshev Avatar asked Nov 24 '16 16:11

apeshev


People also ask

What is the difference between ElementRef and TemplateRef?

ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

What is a TemplateRef?

TemplateReflink Represents an embedded template that can be used to instantiate embedded views. To instantiate embedded views based on a template, use the ViewContainerRef method createEmbeddedView() .

What is the use of TemplateRef?

What is TemplateRef? TemplateRef is a class and the way to reference the ng-template in the component or directive class. Using the TemplateRef we can manipulate the template from component code. To access the above ng-template in the component or directive, first, we need to assign a template reference variable.


1 Answers

As Günter Zöchbauer pointed out, this is not possible and it probably is not too useful as you frequently would want to capture functionality for the template so you will need a Component for that.

However, currently it is not easy to create an isolated component that you just use to get a TemplateRef, as you need a ViewContainerRef in order to do that. Current Angular 2 issue that is targeting that: https://github.com/angular/angular/issues/9293.

like image 126
apeshev Avatar answered Sep 21 '22 09:09

apeshev