1

I am creating a dynamic component angular. How can I catch the error of rendering the template of this component

@ViewChild('vc', { read: ViewContainerRef }) _container: ViewContainerRef;

private insertDynamicComp() {
    this._container.clear();
    this.generateComponent(this.element.data).then((view) => {
      this._container.insert(view);
    });
  }

private generateComponent(template: string): Promise<ViewRef> {
    return new Promise((resolve, reject) => {
      try {
        const _this = this;

        const tmpCmp = Component({
          template: template || '',
          changeDetection: ChangeDetectionStrategy.OnPush,
        })(DynamicClass);
        const tmpModule = NgModule({
          declarations: [tmpCmp],
        })(class {});
        const t = this._compiler.compileModuleAndAllComponentsSync(tmpModule);
        const f = t.componentFactories[0];
        const cmpRef = f.create(this._injector, [], null, this._m);
        cmpRef.instance.SetParentFunction(_this);
        resolve(cmpRef.hostView);
      } catch {
        reject(null);
      }
    });
  }

I would be grateful for any help. Thanks

1 Answer 1

1

solved this problem through ivy render

import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  Injector,
  OnInit,
  ViewChild,
  ɵcompileComponent,
  ɵrenderComponent as renderComponent,
  ɵLifecycleHooksFeature as LifecycleHooksFeature,
  ɵmarkDirty as markDirty,
  Input,
} from '@angular/core'; 
 
const ERROR_TEMPLATE = `
<div style="width:100%; height:100%; border: 1px solid black;border-style:dotted;color: red;font-size: 16px;">
  ERROR TEMPLATE: [error]
</div>`;

@ViewChild('div', { read: ElementRef }) _div: ElementRef;

private generateComponent(template: string): IDynamicClass {
    try {
      class DynamicClass implements IDynamicClass {
        api: any = {};
        parentObj: AngularTemplateComponent;
        mappedValue: IMappedName;
        state: ISimpleStetVar;

        constructor() {}

        public SetParentFunction(parent: AngularTemplateComponent) {
          this.parentObj = parent;
          this.api = parent;
        }
      }

      this.showErrorTemplate = false;

      ɵcompileComponent(DynamicClass, {
        template: template || '',
        changeDetection: ChangeDetectionStrategy.OnPush,
      });

      const comp = renderComponent(DynamicClass, {
        host: this._div.nativeElement,
        injector: this._injector,
        hostFeatures: [LifecycleHooksFeature],
      });

      return comp;
    } catch (error) {
      this.errorTemplate = this._sanitizer.bypassSecurityTrustHtml(
        ERROR_TEMPLATE.replace('[error]', error?.message),
      );
      this.showErrorTemplate = true;
      console.error(error?.message);
      return null;
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.