角度2 NgbModal NgbActiveModal关闭事件模态

时间:2017-06-16 03:20:06

标签: angular ng-bootstrap

我使用Angular 2,我正在使用表单模式,我有两个组件,从一个组件我打开表单模式这样:

import { Component, OnInit, Output} from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { MyFormComponent } from '......./....';


@Component({
    moduleId: module.id,
    selector: 'my-component',
    templateUrl: 'my-component.html'
})
export class MyComponent implements OnInit {

    private anyData: any;
    private anyDataForm: any;


    constructor(
        private modalService: NgbModal
    ) { }

    ngOnInit(): void {
    }

    open() {
        const modalRef = this.modalService.open(MyFormComponent, { size: 'lg' });
        modalRef.componentInstance.anyDataForm = this.anyData;
    }

    possibleOnCloseEvet() { 
        //Do some actions....
    }

}

open()方法从 my-component.html

上的按钮触发

在Form组件(模态组件)上我使用它来关闭实际模态(从它自己)

import { Component, OnInit, OnDestroy, Input } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
    moduleId: module.id,
    selector: 'my-form-component',
    templateUrl: 'my-form-component.html'
})
export class MyFormComponent implements OnInit, OnDestroy {

    @Input() anyDataForm: any;

    constructor(
        public activeModal: NgbActiveModal
    ) {
    }

    ngOnInit(): void {
    }

    //Some form code...

    OnSubmit() {
        this.activeModal.close(); //It closes successfully
    }

    ngOnDestroy(): void {
    }

}

我需要做的是在关闭时解雇某些"调用方组件上的事件仅在模式关闭时才在调用方中执行某些操作。 (不能使用事件发射器)

组件开启器有什么方法可以知道模态何时关闭?我没有找到任何明确的例子。

4 个答案:

答案 0 :(得分:16)

试试这个:

const modalRef = this.modalService.open(MyFormComponent, { size: 'lg' });
modalRef.componentInstance.anyDataForm = this.anyData;

modalRef.result.then((data) => {
  // on close
}, (reason) => {
  // on dismiss
});

答案 1 :(得分:1)

在我的ModalformComponent上

 this.activeModal.close('success');

然后在我的父组件ListComponent上

 this.modalRef = this.modalService.open(ModalformComponent);
 this.modalRef.componentInstance.title = 'Add Record';
 this.modalRef.result.then((result) => {
  if ( result === 'success' ) {
     this.refreshData(); // Refresh Data in table grid
  }
}, (reason) => {
});

答案 2 :(得分:0)

你可以在MyFormComponent中添加

closeModal(){this.activeModal.close(anything); }

答案 3 :(得分:0)

使用this.activeModal.close();代替this.activeModal.dismissAll()。 它将成功关闭弹出窗口。