Last active
May 22, 2017 18:02
-
-
Save hk-skit/7a457fda1957362754f917899937f52b to your computer and use it in GitHub Desktop.
Component Loader Component
This file contains 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 | |
} from '@angular/core'; | |
import { IComponentLoader } from '../component-loader/IComponentLoader'; | |
@Component({ | |
moduleId: module.id, | |
selector: 'a-comp', | |
template: ` | |
<h3>AComponent</h3> | |
<ul> | |
<li *ngFor="let item of data">{{item}}</li> | |
</ul> | |
` | |
}) | |
export class AComponent implements IComponentLoader { | |
@Input() | |
data: any; | |
} |
This file contains 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
<h1> | |
{{title}} | |
</h1> | |
<ng-container *ngFor="let component of components"> | |
<component-loader [component]="component.name" [data]="component.data"></component-loader> | |
</ng-container> |
This file contains 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 { AComponent, BComponent } from './components'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'app works!'; | |
components = [ | |
{ | |
name: AComponent, | |
data: ['Apple', 'Banana', 'Carrot', 'Date'] | |
}, | |
{ | |
name: BComponent, | |
data: ['Apple', 'Banana', 'Carrot', 'Date'] | |
} | |
]; | |
} |
This file contains 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 { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { FormsModule } from '@angular/forms'; | |
import { HttpModule } from '@angular/http'; | |
import { AppComponent } from './app.component'; | |
import { AComponent, BComponent } from './components'; | |
import { ComponentLoaderModule } from './component-loader/component-loader.module'; | |
@NgModule({ | |
declarations: [ | |
AComponent, | |
BComponent, | |
AppComponent | |
], | |
entryComponents: [ | |
AComponent, | |
BComponent | |
], | |
imports: [ | |
BrowserModule, | |
FormsModule, | |
HttpModule, | |
ComponentLoaderModule | |
], | |
providers: [], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains 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 | |
} from '@angular/core'; | |
import { IComponentLoader } from '../component-loader/IComponentLoader'; | |
@Component({ | |
moduleId: module.id, | |
selector: 'b-comp', | |
template: ` | |
<h3>BComponent</h3> | |
<ul> | |
<pre> {{ data | json: 4 }}</pre> | |
</ul> | |
` | |
}) | |
export class BComponent implements IComponentLoader { | |
@Input() | |
data: any; | |
} |
This file contains 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 { Directive, ViewContainerRef } from '@angular/core'; | |
@Directive({ selector: '[componentHost]' }) | |
export class ComponentHostDirective { | |
constructor(public viewContainerRef: ViewContainerRef) { } | |
} |
This file contains 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
<ng-template componentHost></ng-template> |
This file contains 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, | |
AfterViewInit, | |
ViewChild, | |
ComponentFactoryResolver, | |
Input, | |
Type, ViewContainerRef | |
} from '@angular/core'; | |
import { ComponentHostDirective } from '../directive'; | |
@Component({ | |
moduleId: module.id, | |
selector: 'component-loader', | |
templateUrl: 'component-loader.component.html' | |
}) | |
export class ComponentLoaderComponent implements AfterViewInit { | |
@ViewChild(ComponentHostDirective) | |
public componentHost: ComponentHostDirective; | |
/** | |
* Component to be rendered. | |
*/ | |
@Input() | |
public component: Type<any>; | |
/** | |
* Data required by component to be rendered. | |
*/ | |
@Input() | |
public data: any; | |
constructor(private _componentFactoryResolver: ComponentFactoryResolver) { | |
this.renderComponent = this.renderComponent.bind(this); | |
} | |
public ngAfterViewInit() { | |
setTimeout(this.renderComponent); | |
} | |
private renderComponent() { | |
const componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.component); | |
const viewContainerRef = this.componentHost.viewContainerRef; | |
viewContainerRef.clear(); | |
const componentRef = viewContainerRef.createComponent(componentFactory); | |
componentRef.instance.data = this.data; | |
} | |
} |
This file contains 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 { NgModule } from '@angular/core'; | |
import { ComponentLoaderComponent } from './components'; | |
import { ComponentHostDirective } from './directive'; | |
@NgModule({ | |
imports: [], | |
exports: [ | |
ComponentLoaderComponent | |
], | |
declarations: [ | |
ComponentLoaderComponent, | |
ComponentHostDirective | |
], | |
providers: [], | |
}) | |
export class ComponentLoaderModule { } |
This file contains 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
export interface IComponentLoader { | |
data: any; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment