Skip to content

Instantly share code, notes, and snippets.

View natmegs's full-sized avatar

Natalie Smith natmegs

View GitHub Profile
@natmegs
natmegs / some-component.template.html
Created January 8, 2018 23:11
SomeComponent template
<h1>Above the projected content</h1>
<ng-content></ng-content>
<p>Below the projected content</p>
@natmegs
natmegs / app.component.ts
Created January 8, 2018 23:10
*ngTemplateOutlet context object
contextObj = {
donut: "Old fashioned glazed"
};
@natmegs
natmegs / app.template.html
Created January 8, 2018 23:09
*ngTemplateOutlet with context
<ng-container *ngTemplateOutlet="template; context: contextObj">
</ng-container>
<ng-template #template let-food="donut">
{{food}}
</ng-template>
@natmegs
natmegs / app.template.html
Created January 8, 2018 23:08
Using *ngTemplateOutlet
<ng-container *ngTemplateOutlet="template">
</ng-container>
<ng-template #template>
Hello!
</ng-template>
@natmegs
natmegs / app.template.html
Created January 8, 2018 23:00
Using <ng-container>
<ng-container>
You can see me! (Without any help from structural directives)
</ng-container>
<table>
<tbody>
<ng-container *ngFor="let item of items">
<tr *ngIf="item > 1">
<td>{{item}}</td>
</tr>
@natmegs
natmegs / app.template.html
Created January 8, 2018 22:57
Using <ng-template>
<ng-template>
You will never see me :(
</ng-template>
<ng-template *ngIf="showContents">
You still won’t see me (even though I’m using a structural directive, there’s nothing that's telling Angular to use me)
</ng-template>
<ng-template *ngFor="let item of items">
No dice (same reason as *ngIf above)
@natmegs
natmegs / host.component.ts
Created December 20, 2017 23:21
Final host component
import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
import { DynamicComponent } from './dynamic.component';
@Component({
selector: 'host',
templateUrl: './host.component.html',
styleUrls: [ './host.component.css' ],
entryComponents: [DynamicComponent]
})
export class AppComponent {
@natmegs
natmegs / host.component.html
Created December 20, 2017 23:19
Add more controls to view
<label for="name">Enter Component Name</label>
<input type="text" id="name" [(ngModel)]="name" placeholder="Enter Dynamic Component Name">
<br />
<button (click)="addComponent()">Add Component</button>
<button *ngIf="hasDynamicComponents" (click)="removeComponent()">Remove Component</button>
<button *ngIf="hasDynamicComponents" (click)="clearComponents()">Clear All</button>
<ng-container #container></ng-container>
@natmegs
natmegs / host.component.ts
Created December 20, 2017 23:19
Add inputs to dynamic component
import { Component } from '@angular/core';
import { DynamicComponent } from './dynamic.component';
@Component({
selector: 'host',
templateUrl: './host.component.html',
styleUrls: [ './host.component.css' ],
entryComponents: [DynamicComponent]
})
export class AppComponent {
@natmegs
natmegs / host.component.html
Created December 20, 2017 23:17
Add input to view
<label for="name">Enter Component Name</label>
<input type="text" id="name" [(ngModel)]="name" placeholder="Enter Dynamic Component Name">
<br />
<button (click)="addComponent()">Add Component</button>
<ng-container #container></ng-container>