Last active
September 7, 2018 08:15
-
-
Save carmichaelize/f817ad89cc9c9e95b32c01516ebfc621 to your computer and use it in GitHub Desktop.
Angular2 component factory resolver example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
export class ChildBaseComponent { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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;
}