角度2 NgbModal NgbActiveModal关闭事件模态

Dan*_*y G 8 ng-bootstrap angular

我正在使用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....
    }

}
Run Code Online (Sandbox Code Playgroud)

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 {
    }

}
Run Code Online (Sandbox Code Playgroud)

我需要做的是在调用程序组件上触发某种"on close"事件,以便仅在模式关闭时在调用者中执行某些操作.(不能使用事件发射器)

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

Adn*_* A. 18

试试这个:

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

modalRef.result.then((data) => {
  // on close
}, (reason) => {
  // on dismiss
});
Run Code Online (Sandbox Code Playgroud)


小智 6

在我的 ModalformComponent 上

 this.activeModal.close('success');
Run Code Online (Sandbox Code Playgroud)

然后在我的父组件 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) => {
});
Run Code Online (Sandbox Code Playgroud)