Templates
About
Every Angular Component has a template that defines the DOM that the component renders onto the page.
They are usually inside:
*.component.htmlfile.templateproperty of a*.component.tsfile.
How they work
Templates are based on HTML syntax, bu with additional features that are interpreted by Angular.
Angular complies templates into JavaScript in order to build up an internal understanding of your application.
Best Practices
With Template Statements:
Template statements cannot refer to anything in the global namespace like
windowsordocument.They cannot call
console.log()orMath.max().Use short expressions, keep application and business logic in the component.
They should have quick execution, since Angular executes a template expression after every change detection cycle.
Consider caching values when their computation requires greater resources.
When
property binding:Avoid side effects, evaluation of a template expression should have no visible side effects.
Return the proper type, a template expression should result in the type of value that the target property expects.
Keep variables names unique to avoid name collisions or variables shadowing variables in another context.
For
event binding:Prefer to use
codefor binding to keyboard events.
Supported Values & Operators
Whitespaces
Angular collapse multiple consecutive whitespaces in Templates.
To force Angular to preserve these whitespaces specify preserveWhitespaces: true in the @Component decorator.
You use <ng-content> element as a placeholder to mark where content should go.
It works similarly to <slot /> the native component, but with some Angular specific functionality.
Don't conditionally include <ng-content> , for conditional rendering use <ng-template> instead.
<ng-content> itself is not rendered at the resulting DOM.
Fallback Content on <ng-content>
<ng-content>Any added content between <ng-content></ng-content> tags will be used as fallback content if none was passed.
Muliple content placeholder - with select=""
select=""You may project multiple different elements into different <ng-content> placeholders based on CSS selectors.
If a component does not include an <ng-content> placeholder without a select attribute, any elements that don't match one of the component's placeholders do not render into the DOM.
Using select="" for restricting content
select="" for restricting contentThe use of select is not restricted to having multiple <ng-content>, it can be used to be more restrictive to what can go inside <ng-content>.
In fact you may also specify multiple selectors.
Aliasing content for projection ngProjectAs
ngProjectAsngProjectAs is a special attribute, that allows you to specify a CSS selector on any element.
It will be checked against an <ng-content select=""> with the same CSS selector given.
ngProjectAs supports only static values and cannot be bound to dynamic expressions.
This lets you declare a template fragment. (A section of content that you can dynamically or programmatically render)
<ng-template> contents are NOT automatically rendered to the page.
You will need to programatically render it or use specific directives like ngTemplateOutlet.
Create a template fragment with:
Get a reference to a template fragment by:
Declaring a Template Reference Variables on the
<ng-template #variable>.By quering for the frament with
viewChild(TemplateRef).By injecting the fragment in a directive that is applied directly to an
<ng-template>element.
In all three cases, the fragment is represented by a TemplateRef object.
Rendering the Fragments
Once you have the reference to the fragment's TemplateRef object, you can render the fragment in two ways.
Using NgTemplateOutlet
NgTemplateOutletA native directive from Angular that accepts a TemplateRef and renders the fragment as a sibling to the element with the outlet.
The *ngTemplateOutlet is used with a <ng-container>.
This way the content inside <ng-template> is rendered inside <ng-container>.
You can additionally pass parameters accepted by the frament, through a context object corresponding to these paramenters.
And each paramenter is accessed as an attribute prefixed with let- with a value matching a property name in the context object.
Using ViewContainerRef
ViewContainerRefA ViewContainer is a node in Angular's component tree that can contain content.
Any component or directive can inject ViewContainerRef to get a reference to a view container corresponding to that component or directive's location in the DOM.
You then use the createEmbeddedView Angular method on ViewContainerRef to dynamically render a template fragment as the next sibling of the component or directive that injected the ViewContainerRef.
You can additionally pass parameters accepted by the frament, through createEmbeddedView.
And each paramenter is passed as an attribute prefixed with let- with a value matching a property name in the context object.
<ng-container>
<ng-container>It is a special element in Angular that groups multiple elements together or marks a location in a template without rendering a real element in the DOM.
You may apply directives to <ng-container> to add behaviors or configuration to a part of your template.
Angular ignores all attribute bindings and event listeners applied to <ng-container>.
Rendering dynamic components
Use Angular's built-in NgComponentOutlet directive to dynamically render components to the <ng-container>.
Rendering template fragments
Use Angular's built-in NgComponentOutlet directive to dynamically render components to the <ng-container>.
Last updated
