I am creating tab wizard component in Angular 2 by dynamically loading component in tab. I borrowed idea of dynamically loading component from this SO thread. I am also using Angular Material 2 for tab functionality.
My dynamic loader component definition is as follows.
import {Component, ViewChild,ViewContainerRef,Input,
ComponentRef,ComponentResolver,ComponentFactory,
OnInit,OnChanges,AfterViewInit,OnDestroy} from '@angular/core'
import {MD_TAB_GROUP_DIRECTIVES} from '@angular2-material/components/tab-group/tab-group';
import {MdToolbar} from '@angular2-material/components/toolbar/toolbar';
import {MdInput} from '@angular2-material/components/input/input';
import {test} from './test.component'
@Component({
selector: 'wizard',
styleUrls : ['./app/wizard.component.css'],
template: `
<md-tab-group class="demo-tab-group">
<md-tab>
<template md-tab-label>Tab 1</template>
<template md-tab-content>
<div #target></div>
</template>
</md-tab>
</md-tab-group>
`,
directives: [MD_TAB_GROUP_DIRECTIVES, MdToolbar, MdInput],
})
export class wizard {
@ViewChild('target', {read: ViewContainerRef}) target:any;
@Input() type: any|string;
cmpRef:ComponentRef<any>;
private isViewInitialized:boolean = false;
constructor(private resolver: ComponentResolver) {
this.type = test;
console.log("This is loader.");
}
updateComponent() {
if(!this.isViewInitialized) {
return;
}
if(this.cmpRef) {
this.cmpRef.destroy();
}
this.resolver.resolveComponent(this.type).then ((factory:ComponentFactory<any>) => {
this.cmpRef = this.target.createComponent(factory)
});
}
ngOnChanges() {
this.updateComponent();
}
ngAfterViewInit() {
this.isViewInitialized = true;
this.updateComponent();
}
ngOnDestroy() {
if(this.cmpRef) {
this.cmpRef.destroy();
}
}
}
I am getting following error when I run it.
"Error: Uncaught (in promise): TypeError: Unable to get property 'createComponent' of undefined or null reference at resolvePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:538:26) at Anonymous function (http://localhost:3000/node_modules/zone.js/dist/zone.js:574:18) at ZoneDelegate.prototype.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:354:18) at onInvokeTask (eval code:36:25) at ZoneDelegate.prototype.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:354:18) at Zone.prototype.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:256:22) at drainMicroTaskQueue (http://localhost:3000/node_modules/zone.js/dist/zone.js:474:26) at invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:426:22)"
I know its because "target" variable is null and not getting initialized. If I change template in above component like below by taking div tag with #target outside of material design tab component template tag then it works perfectly fine. How to get it working while keeping target div tag still inside material tab template?
template: `
<div #target></div>
<md-tab-group class="demo-tab-group">
<md-tab>
<template md-tab-label>Tab 1</template>
<template md-tab-content></template>
</md-tab>
</md-tab-group>
`,