you can close the popUp from inside or outside
close from inside
//main component
import { Component } from '@angular/core';
import { NbDialogService } from '@nebular/theme';
import { TskPopupComponent } from './tsk-popup/tsk-popup.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
title = 'angularForms';
dialogref: any;
constructor(private dialogService: NbDialogService) {}
open() {
this.dialogService.open(TskPopupComponent, {
hasBackdrop: false,
});
}
}
//popup component
import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { AppComponent } from '../app.component';
@Component({
selector: 'app-tsk-popup',
templateUrl: './tsk-popup.component.html',
styleUrls: ['./tsk-popup.component.scss'],
})
export class TskPopupComponent implements OnInit {
constructor(protected dialogRef: NbDialogRef<any>) {}
close() {
this.dialogRef.close();
}
ngOnInit(): void {}
}
In this case, you can close your component from outside(close popup from any component) but it's a temporary solution
//service.component
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class PopupServiceService {
private modalClosed = false;
private messageSource = new BehaviorSubject(this.modalClosed);
public checkModal: Observable<any> = this.messageSource.asObservable();
checkModalClose(isClose: boolean) {
this.messageSource.next(isClose);
}
constructor() {}
}
//main component
import { Component } from '@angular/core';
import { NbDialogService } from '@nebular/theme';
import { PopupServiceService } from './popup-service.service';
import { TskPopupComponent } from './tsk-popup/tsk-popup.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
title = 'angularForms';
dialogref: any;
constructor(
private dialogService: NbDialogService,
private popupService: PopupServiceService
) {}
open() {
this.dialogref = this.dialogService
.open(TskPopupComponent, {
hasBackdrop: false,
})
.onClose.subscribe((res) => {
console.log('closed');
this.popupService.checkModalClose(false);
});
}
close() {
this.popupService.checkModalClose(true);
}
}
//popup component
import { Component, OnDestroy, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { Subscription } from 'rxjs';
import { AppComponent } from '../app.component';
import { PopupServiceService } from '../popup-service.service';
@Component({
selector: 'app-tsk-popup',
templateUrl: './tsk-popup.component.html',
styleUrls: ['./tsk-popup.component.scss'],
})
export class TskPopupComponent implements OnInit, OnDestroy {
private sub = new Subscription();
constructor(
protected dialogRef: NbDialogRef<any>,
private popupService: PopupServiceService
) {}
close() {
this.dialogRef.close();
}
ngOnInit(): void {
this.sub.add(
this.popupService.checkModal.subscribe((res) => {
if (res === true) {
this.close();
}
})
);
}
ngOnDestroy(): void {
this.sub.unsubscribe();
}
}