如何在Angular 8中捕获动态渲染组件的输出?

netdjw

我想捕获ViewChild渲染组件的输出ngIf发生火灾后,显示了ViewChild的内容

这是我模板的代码:

<div *ngIf="isModalVisible" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <!-- ... -->
      </div>
      <div class="modal-body">
        <ng-template #dialogHost (saveModel)="setModel($event)"></ng-template>
      </div>
    </div>
  </div>
</div>

这是TypeScript文件:

import { ChangeDetectorRef, Component, ComponentFactoryResolver, Input, ViewChild, ViewContainerRef } from '@angular/core';
import { DialogContentItem } from 'src/app/models/dialog/content/dialog-content-item';

@Component({
  selector: 'app-dialog-preparator',
  templateUrl: './dialog-preparator.component.html',
  styleUrls: ['./dialog-preparator.component.css']
})
export class DialogPreparatorComponent {
  isModalVisible = false;
  @Input() dialogContent: DialogContentItem;
  @ViewChild('dialogHost', {static: false, read: ViewContainerRef}) dialogHost: ViewContainerRef;

  showModal() {
    this.isModalVisible = true;
    this.changeDetector.detectChanges();
    this.renderComponent();
  }

  renderComponent() {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.dialogContent.component);

    this.dialogHost.clear();

    const componentRef = this.dialogHost.createComponent(componentFactory);
    (componentRef.instance as DialogContentInterface).data = this.dialogContent.data;
  }

  setModel(model: any) {
    // I need to emit the model
  }
}

这是ProcutCreateEditComponent:

<app-dialog-preparator [dialogContent]="userDialogContent"></app-dialog-preparator>

此组件的TypeScript代码:

import { Component } from '@angular/core';
import { UserCreateEditComponent } from 'src/app/components/user/user-create-edit.component';

@Component({
  selector: 'app-product-create-edit',
  templateUrl: './product-create-edit.component.html',
  styleUrls: ['./product-create-edit.component.scss']
})
export class ProductCreateEditComponent {
  // this could be any other component in my software
  userDialogContent: DialogContentItem = new DialogContentItem(UserCreateEditComponent, {});

  constructor() {}

  // ...
}

这是UserCreateEditComponent的TypeScript代码:

import { Component, Output } from '@angular/core';

@Component({
  selector: 'app-user-create-edit',
  templateUrl: './user-create-edit.component.html',
  styleUrls: ['./user-create-edit.component.scss']
})
export class UserCreateEditComponent {
  @Output() saveModel: EventEmitter<T> = new EventEmitter();
  user = new User();

  constructor() {}

  save() {
    this.saveModel.emit(this.user);
  }
}

因此,在ProcutCreateEditComponent中,我创建了UserCreateEditComponent的一个实例,并将该实例传递给DialogPreparatorComponent。DialogPreparatorComponent在对话框中显示UserCreateEditComponent,然后单击“保存”按钮(在UserCreateEditComponent中),在那里saveModel()向DialogPreparatorComponent发出信号。我认为...但是相反,我得到了这个错误信息:

Event binding saveModel not emitted by any directive on an embedded template.
Make sure that the event name is spelled correctly and all directives are
listed in the "@NgModule.declarations". ("
      </div>
      <div class="modal-body">
        <ng-template #dialogHost [ERROR ->](saveModel)="setModel($event)"></ng-template>
      </div>
    </div>

实际上,这很好,因为ng-template没有saveModel()输出。

但是我该如何捕捉UserCreateEditComponent的输出呢?

啤酒

由于您是用代码创建组件的,因此无法在模板中添加事件处理程序,因此在创建组件时必须将其也添加到组件代码中。

例如,您可以更改渲染组件方法以添加订阅。

renderComponent() {
  const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.dialogContent.component);

  this.dialogHost.clear();

  const componentRef = this.dialogHost.createComponent(componentFactory);
  (componentRef.instance as DialogContentInterface).data = this.dialogContent.data;
  (componentRef.instance as DialogContentInterface).saveModel.subscribe((model) => this.setModel(model));
}

我希望我正确地理解了您的模型,并且您在此处创建的组件是具有output属性的组件。

确保还取消订阅动态组件何时被销毁,以免由于剩余的订阅而导致内存泄漏。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在angular2中动态渲染表单输出

来自分类Dev

如何在Svelte中动态渲染组件?

来自分类Dev

如何在Angular中的另一个动态加载的组件内渲染组件?

来自分类Dev

如何在 React 中动态渲染嵌套组件?

来自分类Dev

如何在Angular的ts文件中销毁动态加载的组件?

来自分类Dev

Angular:了解DI如何在动态加载的组件中工作

来自分类Dev

如何在Angular 8中创建和指向嵌套组件

来自分类Dev

如何显示已在Angular 8中销毁的动态组件?

来自分类Dev

如何在Angular中测试渲染速度

来自分类Dev

如何在 Angular 5 中渲染 PDF?

来自分类Dev

如何在 Angular 中优化渲染?

来自分类Dev

如何在angular 8和bootstrap whit row,col和list字符串中创建动态网格组件?

来自分类Dev

如何在 Angular 2 中通信组件

来自分类Dev

Draft.js 和 stateToHTML,如何在 React 组件中渲染输出 html?

来自分类Dev

如何在Angular中动态滚动<textarea>?

来自分类Dev

如何在动态组件周围包装Angular组件

来自分类Dev

如何在Angular的子组件内部动态创建组件?

来自分类Dev

如何在 angular 中使用动态加载的模块组件中的现有组件

来自分类Dev

如何在render方法中渲染组件?

来自分类Dev

如何在React中渲染递归组件?

来自分类Dev

如何在反应中渲染多个组件?

来自分类Dev

如何在 angular 4 中使用 ngFor 动态渲染 svg

来自分类Dev

渲染完所有组件后,如何在Angular2中运行TypeScript代码?

来自分类Dev

如何在React中渲染动态表

来自分类Dev

如何在Angular 8中循环iframe

来自分类Dev

Angular 8如何在父组件中以不同的动作多次使用组件

来自分类Dev

如何在Angular2中与其动态交互的2个组件中注入相同的服务

来自分类Dev

如何在angular5中从appcomponent动态调用子组件的函数?

来自分类Dev

如何在 Angular 4/5 中动态添加 NG Prime 计划(日历)组件

Related 相关文章

  1. 1

    如何在angular2中动态渲染表单输出

  2. 2

    如何在Svelte中动态渲染组件?

  3. 3

    如何在Angular中的另一个动态加载的组件内渲染组件?

  4. 4

    如何在 React 中动态渲染嵌套组件?

  5. 5

    如何在Angular的ts文件中销毁动态加载的组件?

  6. 6

    Angular:了解DI如何在动态加载的组件中工作

  7. 7

    如何在Angular 8中创建和指向嵌套组件

  8. 8

    如何显示已在Angular 8中销毁的动态组件?

  9. 9

    如何在Angular中测试渲染速度

  10. 10

    如何在 Angular 5 中渲染 PDF?

  11. 11

    如何在 Angular 中优化渲染?

  12. 12

    如何在angular 8和bootstrap whit row,col和list字符串中创建动态网格组件?

  13. 13

    如何在 Angular 2 中通信组件

  14. 14

    Draft.js 和 stateToHTML,如何在 React 组件中渲染输出 html?

  15. 15

    如何在Angular中动态滚动<textarea>?

  16. 16

    如何在动态组件周围包装Angular组件

  17. 17

    如何在Angular的子组件内部动态创建组件?

  18. 18

    如何在 angular 中使用动态加载的模块组件中的现有组件

  19. 19

    如何在render方法中渲染组件?

  20. 20

    如何在React中渲染递归组件?

  21. 21

    如何在反应中渲染多个组件?

  22. 22

    如何在 angular 4 中使用 ngFor 动态渲染 svg

  23. 23

    渲染完所有组件后,如何在Angular2中运行TypeScript代码?

  24. 24

    如何在React中渲染动态表

  25. 25

    如何在Angular 8中循环iframe

  26. 26

    Angular 8如何在父组件中以不同的动作多次使用组件

  27. 27

    如何在Angular2中与其动态交互的2个组件中注入相同的服务

  28. 28

    如何在angular5中从appcomponent动态调用子组件的函数?

  29. 29

    如何在 Angular 4/5 中动态添加 NG Prime 计划(日历)组件

热门标签

归档