ngTemplateOutlet-Angular |
In Angular, we use the ngTemplateOutlet directive to insert an external template into our component's view. This is a powerful feature that allows us to reuse templates and keep our code DRY.
The ngTemplateOutlet directive takes a template reference as its input. This template reference can be a local variable declared in our component's template, or it can be a reference to an external template.
In either case, the ngTemplateOutlet directive will render the template into our view.
We can use the ngTemplateOutlet directive to insert a wide variety of content into our view, including other components.
This makes it an essential tool for building large and complex Angular applications.In Angular, we will show you several ngTemplateOutlet examples that you can use to learn.
Learning objectives
- What is ngTemplateOutlet.
- How to use ngTemplateoutlet in angular.
- ngTemplateOutlet exmaple
- Pass template to a Child Component
- Use ViewChild to Access the template
- Summary
What is ngTemplateOutlet and how it works
How to use ngTemplateoutlet in angular
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-template #estimateTemplate let-lessonsCounter="estimate">
<div> Approximately {{lessonsCounter}} lessons ...</div>
</ng-template>
<ng-container
*ngTemplateOutlet="estimateTemplate;context:ctx">
</ng-container>
`})
export class AppComponent {
totalEstimate = 10;
ctx = {estimate: this.totalEstimate};
}
Check this example on starblitz
import { Component } from '@angular/core';
import { IItem } from './types';
@Component({
selector: 'my-app',
template: `
<label>
Mode:
<select [(ngModel)]="mode">
<option value="card">card</option>
<option value="list">list</option>
</select>
</label>
<card-or-list-view [mode]="mode" [items]="items">
<div *cardItem="let item" style="border: 1px solid #999999; margin: 10px; padding: 10px">
<h1>{{item.header}}</h1>
<p>{{item.content}}</p>
</div>
<li *listItem="let item">
{{item.header}}: {{item.content}}
</li>
</card-or-list-view>
`,
styles: [
`
font-family: Lato;
`
]
})
export class AppComponent {
mode = 'card';
items: IItem[] = [{
header: 'header 1',
content: 'content 1 modified'
}, {
header: 'header 2',
content: 'content 2'
}, {
header: 'header 3',
content: 'content 3'
}, {
header: 'header 4',
content: 'content 4'
}, {
header: 'header 5',
content: 'content 5'
}, {
header: 'Miserable Sunday',
content: 'Sleepy'
}];
}
import { Component, OnInit, ContentChild, TemplateRef, Input } from '@angular/core';
import { CardItemDirective } from '../card-item.directive';
import { ListItemDirective } from '../list-item.directive';
import { IItem } from '../types';
@Component({
selector: 'card-or-list-view',
templateUrl: './card-or-list-view.component.html'
})
export class CardOrListViewComponent implements OnInit {
@Input()
items: IItem[] = [];
@Input()
mode: 'card' | 'list' = 'card';
// Read in our structural directives as TemplateRefs
@ContentChild(CardItemDirective, { read: TemplateRef }) cardItemTemplate;
@ContentChild(ListItemDirective, { read: TemplateRef }) listItemTemplate;
constructor() { }
ngOnInit() {}
}
<ng-container [ngSwitch]="mode">
<ng-container *ngSwitchCase="'card'">
<h1>Card view</h1>
<ng-container *ngFor="let item of items">
<ng-container
*ngTemplateOutlet="cardItemTemplate; context: {$implicit: item, other: 1 }">
</ng-container>
</ng-container>
</ng-container>
<ul *ngSwitchCase="'list'">
<h1>List view</h1>
<ng-container *ngFor="let item of items">
<ng-container *ngTemplateOutlet="listItemTemplate; context: {$implicit: item, other: 2 }"></ng-container>
</ng-container>
</ul>
</ng-container>
Pass template to a Child Component
import { Component, TemplateRef, Input } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<h1>Parent component</h1>
<ng-template #parentTemplate>
<p>
This Template is defined in Parent.
We will send it to child component
</p>
</ng-template>
<app-child [customTemplate]="parentTemplate"></app-child>`
})
export class ParentComponent {
}
And in the child component we can access this template using @Input decorator like this.@Component({
selector: 'app-child',
template: `
<h2>Child component</h2>
<ng-container *ngTemplateOutlet="customTemplate">
</ng-container>
`
})
export class ChildComponent {
@Input() customTemplate: TemplateRef;
}
Use ViewChild to Access the template
import { Component, TemplateRef, Input, OnInit, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<h1>Parent component</h1>
<ng-template #parentTemplate>
<p>
This Template is defined in Parent.
We will send it to child component
</p>
</ng-template>
<app-child [customTemplate]="parentTemplate"></app-child>`
})
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild('parentTemplate',null) myTemplate:TemplateRef;
ngAfterViewInit() {
console.log(this.myTemplate)
}
}
0 Comments