【问题标题】:How to pass data modal Component to parent Component using ng-bootstrap in angular 4如何在角度 4 中使用 ng-bootstrap 将数据模态组件传递给父组件
【发布时间】:2018-02-14 23:35:04
【问题描述】:

我尝试在下面的代码中将数据从模态组件传递到父组件。

package.json

"@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.3"

app.component.html

<button class="btn btn-primary" (click)="openModal()">Open</button>

app.component.ts

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

export class AppComponent {

   constructor(private modalService: NgbModal) { }

    openModal() {
        const modalRef = this.modalService.open(AppModalComponent);
    }
}

app-modal.component.html

<h1>{{data}}</h1>
<button class="btn btn-primary" (click)="addMe()"></button>

app-modal.component.ts

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

export class AppModalComponent {
    private data: string;
    @Output emitService = new EventEmitter();

    constructor() {
        this.data = "hello world";
    }

    addMe() {
        this.emitService.next(this.data)
    }
}

emit之后,如何获取父组件(app.component)的数据??

【问题讨论】:

    标签: angular ng-bootstrap


    【解决方案1】:

    您可以像这样订阅emitService @Output

    openModal() {
         const modalRef = this.modalService.open(AppModalComponent);
         modalRef.componentInstance.emitService.subscribe((emmitedValue) => {
             // do sth with emmitedValue
         });
    }
    

    虽然上述方法可行,但请注意,使用要返回的值关闭模式通常更容易。这将解决modalRef 上可用的承诺。更多详情https://stackoverflow.com/a/43614501/1418796

    【讨论】:

    • 谢谢,这正是我需要解决的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多