Angular 2 - Open/Close default bootstrap modal

前端 未结 5 1785
悲哀的现实
悲哀的现实 2020-12-30 02:15

I do not want to use angular2-bootstrap or ng2-bs3-modal as suggested in the questions/answers Angular 2 Bootstrap Modal and Angular 2.0 and Modal Dialog

Now. I kno

相关标签:
5条回答
  • 2020-12-30 02:45

    try to use ng-window, it's allow developer to open and full control multiple windows in single page applications in simple way, No Jquery, No Bootstrap.

    Avilable Configration

    • Maxmize window
    • Minimize window
    • Custom size,
    • Custom posation
    • the window is dragable
    • Block parent window or not
    • Center the window or not
    • Pass values to chield window
    • Pass values from chield window to parent window
    • Listening to closing chield window in parent window
    • Listen to resize event with your custom listener
    • Open with maximum size or not
    • Enable and disable window resizing
    • Enable and disable maximization
    • Enable and disable minimization
    0 讨论(0)
  • 2020-12-30 02:50

    if your modal has a cancel button (Otherwise create a hidden close button). You can simulate the click event on this button so that your form is closed. Iin your component add a ViewChild

    export class HelloComponent implements OnInit {
    
    @ViewChild('fileInput') fileInput:ElementRef;
    

    in your close button add #fileInput

    <button class="btn btn-danger" #fileInput id="delete-node" name="button" data-dismiss="modal" type="submit">Cancelar</button>
    

    When you want to close the modal programmatically trigger an click event on the close button

    this.fileInput.nativeElement.click();
    

    To open you can use the same idea

    0 讨论(0)
  • 2020-12-30 03:03

    I have done like this and it works for me perfectly.

    var element = document.getElementById("CloseButton") as any;

    element.click(); Where my CloseButton is bootstrap close button

    0 讨论(0)
  • 2020-12-30 03:06

    You can try something like this, create myModal.html:

    <div class="modal-backdrop fade in" [style.display]="showModal ? 'block' : 'none'"></div>
      <div class="modal" tabindex="-1" role="dialog" style="display: block" [style.display]="showModal ? 'block' : 'none'">
         <div class="modal-dialog">
            <div class="modal-popup">
              <div class="popup-title">
                <span>{{title}} </span>
                <i class="icon-cancel fr" data-dismiss="modal" aria-label="Close" (click)="cancelAction()"></i>
                <p *ngIf="subTitle">{{subTitle}}</p>
              </div>
            <ng-content></ng-content>
          </div>
      </div>
    </div>
    

    then, create myModal.component.ts:

    import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
    
    const template: string = require('./myModal.html');
    
    @Component({
       selector: 'modal',
       template
    })
    
    export class Modal implements OnInit {
      @Input('show-modal') showModal: boolean;
      @Input('title') title: string;
      @Input('sub-title') subTitle: string;
      @Input('cancel-label') cancelLabel: string;
      @Input('positive-label') positiveLabel: string;
    
      @Output('closed') closeEmitter: EventEmitter < ModalResult > = new EventEmitter < ModalResult > ();
      @Output('loaded') loadedEmitter: EventEmitter < Modal > = new EventEmitter < Modal > ();
      @Output() positiveLabelAction = new EventEmitter();
    
      constructor() {}
    
      ngOnInit() {
        this.loadedEmitter.next(this);
      }
    
      show() {
        this.showModal = true;
      }
    
      hide() {
        this.showModal = false;
        this.closeEmitter.next({
          action: ModalAction.POSITIVE
        });
      }
    
      positiveAction() {
        this.positiveLabelAction.next(this);
        return false;
      }
    
      cancelAction() {
        this.showModal = false;
        this.closeEmitter.next({
          action: ModalAction.CANCEL
        });
        return false;
      }
    }
    
    export enum ModalAction { POSITIVE, CANCEL }
    
    export interface ModalResult {
      action: ModalAction;
    }
    

    then create module for this so that you can use anywhere and use it anywhere like this:

    <modal #deleteUserModal id="deleteUser"
       [show-modal]="isModalOpen"
       [title]="'Delete'"
       >
      <div class="popup-content">
        <p>Are you sure you want to delete the user permanently?</p>
      </div>
      <div class="popup-footer">
        <button class="btn cancel"  aria-label="Close" (click)="deleteUserModal.hide()">
            Cancel
        </button>
        <button type="button" class="btn btn-primary" (click)="deleteSelectedUser(deleteUserModal)" aria-label="Close">
            Delete
        </button>
       </div>
     </modal>
    

    You can enhance this also :)

    0 讨论(0)
  • 2020-12-30 03:06

    Ok, turns out there's no need to bind to aria-hidden although this should be possible I guess.

    The current answer came from Angular 2.0 and Modal Dialog (but a answer with only 9 upvotes)

    Adding

    <div id="createAccountModal" class="modal fade customForm" role="dialog" [ngClass]="{'in': visibleAnimate}"
           [ngStyle]="{'display': visible ? 'block' : 'none', 'opacity': visibleAnimate ? 1 : 0}">
    

    This to my code, and having a (click) handler on a button toggle the visible and visibleAnimate suited my needs.

    0 讨论(0)
提交回复
热议问题