Javascript 如何在组件内部创建角度模块?
我想在另一个组件中动态创建一个组件。这样我就可以将动态html模板直接传递给decorator,比如这个@Component({ 模板:模板 }); //代码Javascript 如何在组件内部创建角度模块?,javascript,angular,typescript,Javascript,Angular,Typescript,我想在另一个组件中动态创建一个组件。这样我就可以将动态html模板直接传递给decorator,比如这个@Component({ 模板:模板 }); //代码 /** * @param template is html template * @param container is @ViewChild('dynamicFront', { read: ViewContainerRef }) _containerFront: ViewContainerRef; */ private add
/**
* @param template is html template
* @param container is @ViewChild('dynamicFront', { read: ViewContainerRef }) _containerFront: ViewContainerRef;
*/
private addComponent(template, container) {
@Component({
template: template
})
class DynamicComponent {
constructor(public _parent: TemplateEditorComponent) {
this._parent.templateForm.controls['field_heading'].valueChanges.subscribe(value => {
console.log(value);
})
}
}
@NgModule({
imports: [
ReactiveFormsModule,
BrowserModule,
EditorModule
],
declarations: [DynamicComponent, EditableDivDirective, InlineEditorDirective]
})
class DynamicComponentModule { }
const mod = this.compiler.compileModuleAndAllComponentsSync(DynamicComponentModule);
const factory = mod.componentFactories.find((comp) =>
comp.componentType === DynamicComponent
);
const component = container.createComponent(factory);
}
在html组件中,iam显示如下模板
<ng-template #dynamicFront></ng-template>
我遇到了一些构建错误,比如
模板不是字符串,而是全部。目标是显示来自db的原始html,我需要编辑此原始html模板内的文本,并且iam使用tinymce在原始html内进行编辑。如果要求组件的模板是动态的,则应使用带有输入和输出的
或嵌入式组件
如果您不清楚ng模板指令
编辑:
对于一个简单的用例,您的代码相当复杂。如果您所要做的只是根据控制器中的数据呈现不同的模板,那么您应该专注于构建现有模板,使其更具动态性
...
{{value}}
{{value}}
如果需要模板执行某些逻辑,您可以轻松地用组件替换模板:
使用
而不是动态组件。@anlijudavid我刚刚更新了我的帖子。我刚刚更新了我的帖子,请分享你是否有任何解决方案。我已经更新并添加了一些代码,但仍然不清楚你试图实现什么。