In my Angular 4 app, let\'s assume that I\'m inside a service.
At some point, I want to ask the user for a confirmation, currently I\'m doing it with just a co
Try this!!!! Create modal options with a yes function and a no function in the options. Receive args as @Input...
import { Component, OnInit, Input } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
@Component({
selector: 'confirm-box',
templateUrl: './confirm-box.component.html',
styleUrls: ['./confirm-box.component.css']
})
export class ConfirmBoxComponent implements OnInit {
private _args: any;
public get args(): any {
return this._args;
}
@Input()
public set args(value: any) {
this._args = value;
}
constructor(private activeModal: BsModalRef) {
}
ngOnInit(): void {
}
public no(): void {
this.args.noFunction();
this.activeModal.hide();
}
public yes(): void {
this.args.yesFunction();
this.activeModal.hide();
}
}
And then the component or service that launches the dialog:
import { BsModalService, BsModalRef, ModalOptions } from 'ngx-bootstrap/modal';
import { ConfirmBoxComponent } from '../../dialogs/confirm-box/confirm-box.component';
export class AreYouSure {
private activeModal: BsModalRef;
constructor(private modalService: BsModalService) {
//Wait five seconds before launching the confirmbox
setTimeout(()=>{
this.tryLaunchConfirm();
}, 5000);
}
private tryLaunchConfirm(): void {
const config: ModalOptions = {
initialState: {
args: {
title: "Confirm This",
message: "Are you really sure???",
yesFunction: () => { console.log("YES!!!! was clicked."); },
noFunction: () => { console.log("NO!!!! was clicked."); }
}
}
}
this.activeModal = this.modalService.show(ConfirmBoxComponent, config);
}
}