Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active September 7, 2018 08:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmichaelize/f817ad89cc9c9e95b32c01516ebfc621 to your computer and use it in GitHub Desktop.
Save carmichaelize/f817ad89cc9c9e95b32c01516ebfc621 to your computer and use it in GitHub Desktop.
Angular2 component factory resolver example
import { Component } from '@angular/core';
import { ChildBaseComponent } from './child-base.component';
@Component({
template: `This is Child Component 1`
})
export class Child1Component extends ChildBaseComponent {
constructor(){
//Run base constructor
super();
//Child component constructor logic...
console.log('Child Component 1');
}
}
import { Component } from '@angular/core';
import { ChildBaseComponent } from './child-base.component';
@Component({
template: `This is Child Component 2`
})
export class Child2Component extends ChildBaseComponent {
constructor(){
//Run base constructor
super();
//Child component constructor logic...
console.log('Child Component 1');
}
}
import { Component } from '@angular/core';
export class ChildBaseComponent {
}
import {
Component,
Input,
ViewChild,
ViewContainerRef,
ComponentFactoryResolver,
ComponentRef
} from '@angular/core';
import { Child1Component } from './child-1.component';
import { Child2Component } from './child-2.component';
@Component({
selector: 'parent',
template: `
<div #target></div>
`
})
export class ParentComponent {
@Input() child: string;
@Input() value: string;
//Get tag child component will be placed
@ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef;
private componentRef:ComponentRef<any>;
//Child components
private children = {
child1: Child1Component,
child2: Child2Component
};
constructor(private compiler: ComponentFactoryResolver) {}
//Pass through value to child component
renderComponent(){
if (this.componentRef) this.componentRef.instance.value = this.value;
}
//Compile child component
ngAfterContentInit() {
let childComponent = this.children[this.child || 'child1'];
//Resolve child component
let componentFactory = this.compiler.resolveComponentFactory(childComponent);
this.componentRef = this.target.createComponent(componentFactory);
this.renderComponent();
}
//Pass through value to child component when value changes
ngOnChanges(changes: Object) {
this.renderComponent();
}
}
@nidhiparikh5294
Copy link

import {
Component,
Input,
ViewChild,
ViewContainerRef,
ComponentFactoryResolver,
ComponentRef
} from '@angular/core';

import { Child1Component } from './child-1.component';
import { Child2Component } from './child-2.component';

@component({
selector: 'parent',
template: <div #target></div>
})
export class ParentComponent {
@input() child: string;
@input() value: string;

//Get tag child component will be placed
@ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef;
private componentRef:ComponentRef<any>;

//Child components
private children = {
	child1: Child1Component,
	child2: Child2Component
};

constructor(private compiler: ComponentFactoryResolver) {}

//Pass through value to child component
renderComponent(){
	if (this.componentRef) this.componentRef.instance.value = this.value;
}

//Compile child component
ngAfterContentInit() {
	let childComponent = this.children[this.child || 'child1'];
    //Resolve child component
    let componentFactory = this.compiler.resolveComponentFactory(childComponent);
    this.componentRef = this.target.createComponent(componentFactory);
    this.renderComponent();
}

//Pass through value to child component when value changes
ngOnChanges(changes: Object) {
	this.renderComponent();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment