【问题标题】:Open Modal in parent component from child component从子组件在父组件中打开 Modal
【发布时间】:2021-06-16 10:49:39
【问题描述】:

我在 Angular 项目中有一个父组件和一个子组件,我需要从子打开一个放置在父组件中的模态,所以我使用 ng-Zorro,如果我有类似的代码用于打开父组件中的modal:

家长: 从'@angular/core'导入{组件};

@Component({
  selector: 'nz-demo-modal-basic',
  template: `
    <button nz-button [nzType]="'primary'" (click)="showModal()"><span>Show Modal</span></button>
    <nz-modal [(nzVisible)]="isVisible" nzTitle="The first Modal" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()">
      <ng-container *nzModalContent>
        <p>Content one</p>
        <p>Content two</p>
        <p>Content three</p>
      </ng-container>
    </nz-modal>
  `
})
export class NzDemoModalBasicComponent {
  isVisible = false;

  constructor() {}

  showModal(): void {
    this.isVisible = true;
  }

  handleOk(): void {
    console.log('Button ok clicked!');
    this.isVisible = false;
  }

  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }
}

我应该怎么做才能从孩子打开相同的模式? 是相关的@Input 和@Output 吗? 我也想知道它,因为我不想在 de 项目中有重复的代码,所以服务也可以对它有用吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    我认为实现它的首选方法是在子组件中添加@Output,这将触发父组件中的showModal 方法调用。

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      相关资源
      最近更新 更多