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.
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.
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With