Answer the question
In order to leave comments, you need to log in
Why is not a known element when creating a component dynamically?
Here is the template:
<ng-container *ngTemplateOutlet="staticForm"></ng-container>
<template #formDynamic></template>
<ng-template #staticForm>
<form class="container" [formGroup]="testForm">
<tui-text-area formControlName="value">Введите текст</tui-text-area>
</form>
</ng-template>
compileFromRaw(template: string, data: any): ViewRef {
const styles: string[] = [];
function TmpCmpConstructor() {
// @ts-ignore
this.data = data;
}
// @ts-ignore
const templateComponent = Component({template, styles})(new TmpCmpConstructor().constructor);
const tmpModule = NgModule({
imports: [],
declarations: [templateComponent],
})(class {
});
const factories = this.compiler.compileModuleAndAllComponentsSync(tmpModule)
const f = factories.componentFactories[0];
this.componentRef = f.create(this.injector, [], null, this.moduleRef);
this.componentRef.instance.name = 'my-dynamic-component';
return this.componentRef.hostView;
}
this.container.insert(this.compiler.compileFromRaw(
'<form class="container" [formGroup]="testForm">\n' +
' <tui-text-area formControlName="value">{{data}}</tui-text-area>\n' +
' </form>',
'Введите текст 2',
));
NG0304: 'tui-text-area' is not a known element
static version was displayed correctly! Answer the question
In order to leave comments, you need to log in
Some code is a bit ugly. Especially if it will be supported after you. It is better to copy one to one from the docks, there are examples .
I don't know what is statically displayed there, but the error indicates that tui-text-area is not declared. tui-text-area must be added to the declarations of the module, or the module in which this component is connected must be imported.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question