在Angular中替换$ compile [英] Replacement of $compile in Angular
本文介绍了在Angular中替换$ compile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Angular 4中编译一些HTML内容,这可以通过$ compile在Angular 1中完成.
I am trying to compile some HTML content in Angular 4, which can be done by $compile in Angular 1.
我尝试了一些不建议使用的Angular 2替代方案.
I tried some suggested alternatives of Angular 2, which are also deprecated.
有人建议我做最好的方法.
Some one suggest me the best way to do.
推荐答案
The replacement of $compiler
service in Angular is a Compiler. You can inject it like this:
class MyComponent {
constructor(compiler: Compiler) {}
阅读文章您需要了解Angular中的动态组件,以了解如何将其用于动态编译组件.
Read the article Here is what you need to know about dynamic components in Angular to understand how it can be used to dynamically compile components.
这是文章中的一个示例:
Here is an example from the article:
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';
})
}
这篇关于在Angular中替换$ compile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文