【问题标题】:Event emitter from bootstrap modal to parent从引导模式到父级的事件发射器
【发布时间】:2017-03-08 20:42:00
【问题描述】:

我想将一个模态事件从模态组件传递给模态组件的父组件,但由于某种原因,我似乎无法让 EventEmitter 工作。如果有人有想法,将不胜感激。主要代码如下,来自 ng-bootstrap 演示的(非工作)plunk 在这里:http://plnkr.co/edit/xZhsqBV2mQaPxqFkoE7C?p=preview

app.ts

@Component({
  selector: 'my-app',
  template: `
    <div class="container-fluid" (notifyParent)="getNotification($event)">
    <template ngbModalContainer></template>
    <ngbd-modal-component ></ngbd-modal-component>
  </div>
  `
})
export class App {
      getNotification(evt) {
        console.log('Message received...');
    }
}

模态组件.ts

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

import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'ngbd-modal-content',
  template: `
    <div class="modal-body">
      <p>Hello, {{name}}!</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
      <button type="button" class="btn btn-secondary" (click)="notify()">Notify</button>
    </div>
  `
})
export class NgbdModalContent {
  @Input() name;
  @Output() notifyParent: EventEmitter<any> = new EventEmitter();
  constructor(public activeModal: NgbActiveModal) {}

      notify(){
        console.log('Notify clicked...');
        this.notifyParent.emit('Here is an Emit from the Child...');
    }
}

@Component({
  selector: 'ngbd-modal-component',
  templateUrl: 'src/modal-component.html'
})
export class NgbdModalComponent {
  constructor(private modalService: NgbModal) {}

  open() {
    const modalRef = this.modalService.open(NgbdModalContent);
    modalRef.componentInstance.name = 'World';
  }
}

【问题讨论】:

标签: angular ng-bootstrap


【解决方案1】:

在 Angular 7 中接受的答案不起作用。所以我找到了新的解决方案。它与那里相似但略有变化。

不应更改子组件的打字稿文件中的任何内容。您需要为@outputemit 函数执行常规代码。但是您需要对父类型脚本文件进行一些更改。下面的代码正在为我收集事件和数据工作。

open() {
  const modalRef = this.modalService.open(NgbdModalContent);
  // componentInstace didnot work here for me
  // modalRef.componentInstance.name = 'World';
  modalRef.content.notifyParent.subscribe((result)=>{
       console.log(result);   
  })
}     

【讨论】:

    【解决方案2】:

    更新答案:

    我终于找到了解决您问题的方法。我不确定您是否仔细研究了 ng-bootstrap 网站上为 modal component 提供的所有示例。

    您需要使用内容组件中的 activeModal.close() 方法返回结果。该值将在模态组件中获取,然后您可以使用它做任何您想做的事情。我创建了一个working Plunker,它基本上是官方 plunk 的分支,它的作用就像魅力一样。

    旧答案:

    我认为您将事件处理代码放在错误的位置,这就是您没有收到事件通知的原因。

    下面是 app.ts 上的工作模板

      template: `
      <div class="container-fluid">
        <template ngbModalContainer></template>
        <ngbd-modal-component (notifyParent)="getNotification($event)"></ngbd-modal-component>
      </div>
      `
    

    还将modal-component.ts中Notify函数的代码修改为

      notify(){
        console.log('Notify clicked...');
        this.notifyParent.emit('Here is an Emit from the Child...');
        this.activeModal.close('Notify click');
    }
    

    我已经对你的 plunk 进行了分叉和修改,使其能够正常工作 here

    【讨论】:

    • 我知道你要去哪里,但我遇到的麻烦是在模态的父级中冒泡到 getNotification() 。一个简单的例子可能是如果父母有一个表格并且对话是一个明确的表格是或否。几个小时前做这件事似乎很简单..
    • @Neph 我认为这很容易,但您只需要多尝试一下。请检查我更新的答案,它给出了模态组件和模态组件父级的结果。
    【解决方案3】:

    我在 Angular 7 上,我在父级中处理来自子级的事件的方式就像 @Veshraj Joshi 所做的那样。如下:

    `modalRef.componentInstance.notifyParent.subscribe(result => {
      console.log(result);
    });`
    

    【讨论】:

      【解决方案4】:

      这就是你的 app.ts 应该是这样的:

      @Component({
        selector: 'my-app',
        template: `
          <div class="container-fluid">
            <template ngbModalContainer></template>
            <ngbd-modal-component>
                <ngbd-modal-content [name]="modal Title" (notifyParent)="getNotification($event)"></ngbd-modal-content>
            </ngbd-modal-component>
        </div>
        `
      })
      export class App {
            getNotification(event) {
              console.log('Message received...');
          }
      } 

      【讨论】:

        猜你喜欢
        • 2017-12-30
        • 1970-01-01
        • 2017-03-07
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        相关资源
        最近更新 更多