question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: change NgbModalRef's options after the modal is opened

See original GitHub issue

It would be nice to be able to change the properties of the opened modal such as backdrop or beforeDismiss. I need to change the backdrop based on a conditional promise so that the user cannot close the modal when such condition is true.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
benouatcommented, Jan 9, 2019

Another try 😃

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

@Component({
  selector: 'ngbd-modal-content',
  template: `
    <div class="modal-header">
      <h4 class="modal-title">Hi there!</h4>
      <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Hello, {{name}}!</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-dark" (click)="closeWithAnimation()">{{allowDismiss ? 'Close' : 'Please wait 5 seconds...'}}</button>
    </div>
  `
})
export class NgbdModalContent {
  @Input() name;

  allowDismiss = true;

  constructor(public activeModal: NgbActiveModal) {}

  closeWithAnimation() {
    if (this.allowDismiss) {
      console.log('preventing modal to be dismissed');
      this.allowDismiss = false;

      // Do your post request in here
      setTimeout(() => {
        this.allowDismiss = true;
        console.log('modal can be dismissed again');
      }, 5000);
    }
  }
}

@Component({
  selector: 'ngbd-modal-component',
  templateUrl: './modal-component.html'
})
export class NgbdModalComponent {
  constructor(private modalService: NgbModal) {}

  open() {
    const modalRef = this.modalService.open(NgbdModalContent, {
      beforeDismiss: () => modalRef.componentInstance.allowDismiss
    });
    modalRef.componentInstance.name = 'World';
  }
}

See live version here: https://stackblitz.com/edit/angular-6xqy8s-modal-prevent-dismiss

1reaction
EpaXapatecommented, Dec 5, 2018

@benouat Not for my specific use case. What I need is to open the modal like you normalli would and once inside said modal I would make a post request. While that promise is resolving I need to disable the backdrop and keyboard click events so the user can’t close the modal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practice for calling the NgbModal open method
I've got an impression that you are looking for the feature that is planned but not implemented yet - ability to open a...
Read more >
Modal - Angular powered Bootstrap
open. open(content: any, options: NgbModalOptions) => NgbModalRef. Opens a new modal window with the specified content and supplied options.
Read more >
Using a bootstrap modal as a dynamic component - Nishu Goel
So let us start by creating an Info modal component which we will render only when we require it on an operation in...
Read more >
Implementing Modal Dialog Functions With Promise-based ...
Learn how to implement modal dialog functions with promise-based dialog results in your Angular application. See more from Wijmo today.
Read more >
Writing a Generic Type-Safe ng-bootstrap NgbModal Launcher
For an Angular project for one of our clients, I've recently started using ng-bootstrap to implement standard modal dialogs in a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found