When it comes to replacing the $compiler
service in Angular, the go-to solution is utilizing the Compiler service. To implement this, you can simply inject it into your code like so:
class MyComponent {
constructor(compiler: Compiler) {}
If you want a more comprehensive understanding of how the Compiler service can be used for dynamically compiling components, take a look at the insightful article titled Here is what you need to know about dynamic components in Angular.
An illustrative example from the aforementioned article showcases:
ngAfterViewInit() {
const template = '<span>generated on the fly: {{name}}</span>';
const tmpCmp = Component({template: template})(class {
});
const tmpModule = NgModule({declarations: [tmpCmp]})(class {
});
this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = this.vc.createComponent(tmpCmp);
cmpRef.instance.name = 'dynamic';
})
}