I am currently developing an Angular 2 app in RC5 where I need to dynamically load Components.
In the past, in RC1, I achieved this by using dynamicComponentLoader as shown below:
@Component({
moduleId: module.id,
selector: 'generic-component',
templateUrl: 'generic.component.html',
styleUrls: ['generic.component.css']
})
export class GenericComponent implements OnInit {
@ViewChild('target',{read:ViewContainerRef}) target;
@Input('component-name') componentName:string;
@Input('component-info') componentInfo:string;
@Input('component-model') componentModel:Object;
keysRegister:string[]=[
'users',
'employee'
];
componentNames:string[]=[
'UserComponent',
'EmpComponent'
];
constructor(private dcl:DynamicComponentLoader) {}
ngOnInit() {
}
ngAfterViewInit(){
let componentIndex=this.keysRegister.indexOf(this.componentName);
this.dcl.loadNextToLocation(StandardLib[this.componentNames[componentIndex]],this.target)
.then(ref=>{
ref.instance.componentModel=this.componentModel;
});
console.log("GenericComponent...... "+this.componentName+" Loaded");
}
}
Whenever I needed to load the component, I simply did the following:
<div *ngIf="columnModel" style="border:1px solid orange">
<generic-component
[component-name]="columnModel.componentName" <!-- Here I would pass *users* -->
[component-model]="columnModel.componentModel"
[component-info]="columnModel.componentInfo"
></generic-component>
</div>
I attempted using componentResolver but encountered issues getting it to work properly. Any suggestions or guidance would be greatly appreciated. Thank you.