typescript - Angular 2/4 component with dynamic template or templateUrl -
i have been trying find solution everywhere.
i have project different 'skins', different sets of templates/css.
i trying have components use skin based on variable theme_dir.
unfortunately, cannot find how make happens. looked dynamic component loader on angular.io without success.
i looked @ few answers here without success either.
does have idea?
this tried far:
import { componentfactoryresolver, viewcontainerref } '@angular/core'; // @component({ // templateurl: '../../assets/theme/'+theme_dir+'/login.template.html', // }) export class logincomponent implements, afterviewinit { private log = log.create('loginpage'); constructor(private mzkslsrequestservice: mzklsrequestservice, private componentfactoryresolver: componentfactoryresolver, public viewcontainerref: viewcontainerref) { } ngafterviewinit() { let componentfactory = this.componentfactoryresolver.resolvecomponentfactory(new component({ templateurl: '../../assets/theme/default/login.template.html', })); let viewcontainerref = this.viewcontainerref; viewcontainerref.clear(); let componentref = viewcontainerref.createcomponent(componentfactory); } }
you can this:
import { compiler, component, injector, version, viewchild, ngmodule, ngmoduleref, viewcontainerref } '@angular/core'; @component({ selector: 'my-app', template: ` <h1>hello {{name}}</h1> <ng-container #vc></ng-container> ` }) export class appcomponent { @viewchild('vc', {read: viewcontainerref}) vc; name = `angular! v${version.full}`; constructor(private _compiler: compiler, private _injector: injector, private _m: ngmoduleref<any>) { } ngafterviewinit() { const tmpcmp = component({ moduleid: module.id, templateurl: './e.component.html'})(class { }); const tmpmodule = ngmodule({declarations: [tmpcmp]})(class { }); this._compiler.compilemoduleandallcomponentsasync(tmpmodule) .then((factories) => { const f = factories.componentfactories[0]; const cmpref = f.create(this._injector, [], null, this._m); cmpref.instance.name = 'dynamic'; this.vc.insert(cmpref.hostview); }) } } just make sure url correct , template loaded client.
read here need know dynamic components in angular more details.
Comments
Post a Comment