I am trying to switch between two different sidebars based on a variable obtained from the URL. However, when I attempt to implement this feature, I encounter an error message.
@Component({
selector: 'sidebar',
templateUrl: './sidebar.component.html',
styleUrls: [
'./sidebar.component.css'
]
})
Here is my current implementation:
import {
Compiler, Component, Injector, VERSION, ViewChild, NgModule, NgModuleRef,
ViewContainerRef, AfterViewInit, OnInit
} from '@angular/core';
@Component({
selector: 'sidebar',
templateUrl: '<ng-container #dynamicTemplate></ng-container>',
styleUrls: [
'./sidebar.component.css'
]
})
export class SidebarComponent implements OnInit {
@ViewChild('dynamicTemplate', {read: ViewContainerRef}) dynamicTemplate;
constructor(
private _compiler: Compiler,
private _injector: Injector,
private _m: NgModuleRef<any>
) {}
ngAfterViewInit() {
let myTemplateUrl = './sidebar.component.html';
if (this.modee === 'char') {
myTemplateUrl = './Othersidebar.component.html';
}
const tmpCmp = Component({
moduleId: module.id, templateUrl: myTemplateUrl
})(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.dynamicTemplate.insert(cmpRef.hostView);
});
}
However, upon testing this code, I receive the following error message:
client?afea:119 ..../app/sidebar.component.ts
Module not found: Error: Can't resolve './<ng-container #dynamicTemplate></ng-container>' in '.....\app\sidebar'
@ ./src/main/webapp/app/layouts/sidebar/sidebar.component.ts 303:18-77
@ ./src/main/webapp/app/layouts/index.ts
@ ./src/main/webapp/app/app.module.ts
@ ./src/main/webapp/app/app.main.ts
@ multi (webpack)-dev-server/client?http://localhost:9060 webpack/hot/dev-server ./src/main/webapp/app/app.main
Can anyone provide a solution to this issue and explain what may be causing this error? Thank you.