Sr. Technology Consultant, Virtusa
It is technique of loading relevant resources on demand
pankajparkar
pankajparkar
@ViewChild('dynamicComponent', { read: ViewContainerRef }) dynamicComponent: any;
constructor(
private factoryResolver: ComponentFactoryResolver
) { }
showBusinessCard() {
this.clear();
const compFactory = this.factoryResolver.resolveComponentFactory(BusinessCardComponent);
const compRef = this.dynamicComponent.createComponent(compFactory);
compRef.instance.profile = this.profile;
}
clear() {
this.dynamicComponent.clear();
}
<button (click)="showBusinessCard()">
Show Business Card
</button>
<ng-container #dynamicComponent></ng-container>
pankajparkar
Weather Forecast Widget
pankajparkar
Source - https://github.com/pankajparkar/weather-cast
/* imports */
@Component({
selector: 'wc-weather-dashboard',
templateUrl: './weather-dashboard.component.html',
styleUrls: ['./weather-dashboard.component.css']
})
export class WeatherDashboardComponent implements OnInit, OnDestroy {
@Input() headingStart = 'Weather Forecast in';
...
}
// important to export NgModule from component file
@NgModule({
declarations: [
WeatherDashboardComponent,
...
],
imports: [
...
],
exports: [WeatherDashboardComponent]
})
export class WeatherForecastModule { }
pankajparkar
@ViewChild('container', { read: ViewContainerRef }) container: any;
constructor(
private factory: ComponentFactoryResolver
) { }
async loadWeatherWidget() {
this.container.clear();
const weatherComponent =
await import('../weather-forecast/weather-dashboard/weather-dashboard.component')
.then(i => i.WeatherDashboardComponent);
const weatherForecastWidget = this.factory.resolveComponentFactory(weatherComponent);
const componentRef = this.container.createComponent(weatherForecastWidget);
componentRef.instance.headingStart = 'Just for fun';
}
<button (click)="loadWeatherWidget()">
Load Weather Widget
</button>
<ng-container #container></ng-container>
pankajparkar
(*ngComponentOutlet)
<form (ngSubmit)="submit()">
<h3>Feedback Form</h3>
<div *ngFor="let control of controls">
<ng-container
*ngComponentOutlet="control.component | async">
</ng-container>
</div>
<button mat-raised-button>
Submit
</button>
</form>
controls: {component?: Promise<any>|null}[] = [
{ component: import('./controls/currency/currency.component').then(c => c.CurrencyComponent) },
{ component: import('./controls/number/number.component').then(c => c.NumberComponent) },
{ component: import('./controls/select/select.component').then(c => c.SelectComponent) },
{ component: import('./controls/slider/slider.component').then(c => c.SliderComponent) },
];
pankajparkar
(ng-dynamic-componet)
componentsMap: {[key: string]: Promise<any>|null} = {
currency: import('./../controls/currency/currency.component').then(m => m.CurrencyComponent),
number: import('./../controls/number/number.component').then(m => m.NumberComponent),
select: import('./../controls/select/select.component').then(m => m.SelectComponent),
slider: import('./../controls/slider/slider.component').then(m => m.SliderComponent),
};
controls: Controls[] = [
{ type: 'currency',inputs:{value: 1000, formControlName: 'price', group: this.feedbackForm}},
{ type: 'number',inputs:{formControlName: 'age', value: 100, group: this.feedbackForm}},
{ type: 'select',inputs:{options: ['Mumbai', 'Pune'], formControlName: 'city', value: 'Mumbai'}},
{ type: 'slider',inputs:{formControlName: 'rating', value: 1, group: this.feedbackForm}},
];
constructor() { }
createForm(controls: Controls[] = []) {
controls.forEach(control => {
this.feedbackForm.addControl(
control.inputs.formControlName,
new FormControl(control.inputs.value || '')
);
});
}
pankajparkar
(ng-dynamic-componet)
<form [formGroup]="feedbackForm" (ngSubmit)="submit()">
<h3>Profile Form</h3>
<div *ngFor="let control of controls">
<ng-template [ngComponentOutlet]="componentsMap[control.type] | async"
[ndcDynamicInputs]="control.inputs"
></ng-template>
</div>
<button mat-raised-button>
Submit
</button>
</form>
pankajparkar
pankajparkar
Source - https://github.com/angular/angular/pull/42831
pankajparkar
pankajparkar
pankajparkar
Emulating tree-shakable components using single component Angular modules
Weather App - https://github.com/pankajparkar/weather-cast