Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 ng-template documentation and usage

Tags:

angular

I updated my project to the latest angular2 4.0.0-rc.5 and upon building it spits a lot of warnings like this:

Template parse warnings:
The <template> element is deprecated. Use <ng-template> instead ("_newGroupReward.group.description}}

My question is - what is and where can I find info about it and how to use it ? I have been googling however the only doc I could find is about the stable 2.4.x versions ( official docs ).

Best regards

like image 577
mp3por Avatar asked Mar 21 '17 12:03

mp3por


People also ask

What is ng-template used for?

ng-template is an Angular element that is used for rendering HTML in a template. However, it is not rendered directly on DOM. If you include an ng-template tag to a template, the tag and the content inside it will be replaced by comment upon render.

What is difference between template and ng-template?

ng-template is used to render HTML in a template but is never rendered directly. If you add a ng-template tag to your template, it and everything inside it will be replaced by a comment. It might seem a bit useless, but it is rarely used alone. It can be for example used to define the else case of an *ngIf.

What is difference between ng-template and Ng container?

ng-container serves as a container for elements which can also accept structural directives but is not rendered to the DOM, while ng-template allows you to create template content that is not rendered until you specifically (conditionally or directly) add it to the DOM.

Is ng-template a structural directive?

Structural Directives are directives which change the structure of the DOM by adding or removing elements. There are three built-in structural directives, NgIf , NgFor and NgSwitch . These directives work by using the HTML 5 <ng-template> tag.


1 Answers

ngTemplate is a replacement for template, It is used when a sample template should be injected into the DOM.

The below example is illustrated with using ngIf else as below,

<ng-template #loading>Failed to do something wrong...</ng-template>
<div *ngIf="userObservable;else loading;">
  Aravind is here
</div>

When I am using loading, angular calls the TemplateRef with the name as loading denoted by #loading and respective template is replaced inside the div.

LIVE DEMO

Documentation link

like image 51
Aravind Avatar answered Feb 02 '23 19:02

Aravind