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

问题描述 投票:0回答:1

我想捕获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的输出?

angular typescript angular-components eventemitter angular-event-emitter
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.