linkNgTemplateOutlet
| npm Package | @angular/common |
|---|---|
| Module | import { NgTemplateOutlet } from '@angular/common'; |
| Source | common/src/directives/ng_template_outlet.ts |
| NgModule | CommonModule |
Inserts an embedded view from a prepared TemplateRef
linkOverview
@Directive({ selector: '[ngTemplateOutlet]' })
class NgTemplateOutlet implements OnChanges {
ngTemplateOutletContext: Object
ngTemplateOutlet: TemplateRef<any>
ngOnChanges(changes: SimpleChanges)
}
linkHow To Use
<ng-container *ngTemplateOutlet="templateRefExp; context: contextExp"></ng-container>
linkSelectors
[ngTemplateOutlet]
linkInputs
ngTemplateOutletContext bound to NgTemplateOutlet.ngTemplateOutletContext
linkDescription
You can attach a context object to the EmbeddedViewRef by setting [ngTemplateOutletContext].
[ngTemplateOutletContext] should be an object, the object's keys will be available for binding
by the local template let declarations.
Note: using the key $implicit in the context object will set its value as default.
linkExample
@Component({
selector: 'ng-template-outlet-example',
template: `
<ng-container *ngTemplateOutlet="greet"></ng-container>
<hr>
<ng-container *ngTemplateOutlet="eng; context: myContext"></ng-container>
<hr>
<ng-container *ngTemplateOutlet="svk; context: myContext"></ng-container>
<hr>
<ng-template #greet><span>Hello</span></ng-template>
<ng-template #eng let-name><span>Hello {{name}}!</span></ng-template>
<ng-template #svk let-person="localSk"><span>Ahoj {{person}}!</span></ng-template>
`
})
class NgTemplateOutletExample {
myContext = {$implicit: 'World', localSk: 'Svet'};
}
linkConstructor
constructor(_viewContainerRef: ViewContainerRef)
linkMembers
ngTemplateOutlet: TemplateRef<any>
ngOnChanges(changes: SimpleChanges)