How to use code to open a modal in Angular 2?

前端 未结 14 2970
天涯浪人
天涯浪人 2020-12-05 04:34

Usually we use data-target=\"#myModal\" in the

相关标签:
14条回答
  • 2020-12-05 04:46

    I'm currently using Bootstrap 4.3 in Angular 8 and want to open modal window programmatically (without actually clicking on some button as the official demo shows).

    Following method works for me: The general idea is to create a button associated to a modal window. First make sure after you click this button, it can open the modal. Then give this button tag an id using hashtag, for example #hiddenBtn. In component ts file, import ViewChild from @angular/core and write below code:

    @ViewChild('hiddenBtn', {static: false}) myHiddenBtn;
    

    After that, whenever you want to open this modal window in your component ts code, write following code to simulate click operation

    this.myHiddenBtn.nativeElement.click();
    
    0 讨论(0)
  • 2020-12-05 04:49

    I am using a variable to control show and hide and rely on the button the open the modal

    ts code:

    showModel(){
      this.showModal = true;
    }
    

    html:

    <button type="button" data-toggle="modal" data-target="#myModal" (click)="showModel()>Open Modal</button>
    
    <div *ngIf="showModal" >
      <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Some text in the modal.</p>
                </div>
            </div>
        </div>
    </div>
    

    0 讨论(0)
  • 2020-12-05 04:50

    This is one way I found. You can add a hidden button:

    <button id="openModalButton" [hidden]="true" data-toggle="modal" data-target="#myModal">Open Modal</button>
    

    Then use the code to "click" the button to open the modal:

    document.getElementById("openModalButton").click();
    

    This way can keep the bootstrap style of the modal and the fade in animation.

    0 讨论(0)
  • 2020-12-05 04:52

    Best way I have found. Put #lgModal or some other variable name in your modal.

    In your view:

    <div bsModal #lgModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" (click)="lgModal.hide()" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">Large modal</h4>
          </div>
          <div class="modal-body">
            ...
          </div>
        </div>
      </div>
    </div>
    

    In your component

    import {Component, ViewChild, AfterViewInit} from '@angular/core';
    import {CORE_DIRECTIVES} from '@angular/common';
    // todo: change to ng2-bootstrap
    import {MODAL_DIRECTIVES, BS_VIEW_PROVIDERS} from 'ng2-bootstrap/ng2-bootstrap';
    import {ModalDirective} from 'ng2-bootstrap/ng2-bootstrap';
    
    
    @Component({
      selector: 'modal-demo',
      directives: [MODAL_DIRECTIVES, CORE_DIRECTIVES],
      viewProviders:[BS_VIEW_PROVIDERS],
      templateUrl: '/app/components/modals/modalDemo.component.html'
    })
    export class ModalDemoComponent implements AfterViewInit{
    
      @ViewChild('childModal') public childModal: ModalDirective;
      @ViewChild('lgModal') public lgModal: ModalDirective;
    
      public showChildModal():void {
        this.childModal.show();
      }
    
      public hideChildModal():void {
        this.childModal.hide();
      }
    
      ngAfterViewInit() {
          this.lgModal.show();
      }
    
    }
    
    0 讨论(0)
  • 2020-12-05 04:53

    Think I found the correct way to do it using ngx-bootstrap. First import following classes:

    import { ViewChild } from '@angular/core';
    import { BsModalService, ModalDirective } from 'ngx-bootstrap/modal';
    import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
    

    Inside the class implementation of your component add a @ViewCild property, a function to open the modal and do not forget to setup modalService as a private property inside the components class constructor:

    @ViewChild('editSomething') editSomethingModal : TemplateRef<any>;
    ...
    
    modalRef: BsModalRef;
    openModal(template: TemplateRef<any>) {
      this.modalRef = this.modalService.show(template);
    }
    ...
    constructor(
    private modalService: BsModalService) { }
    

    The 'editSomething' part of the @ViewChild declaration refers to the component template file and its modal template implementation (#editSomething):

    ...
    <ng-template #editSomething>
      <div class="modal-header">
      <h4 class="modal-title pull-left">Edit ...</h4>
      <button type="button" class="close pull-right" aria-label="Close" 
       (click)="modalRef.hide()">
        <span aria-hidden="true">&times;</span>
      </button>
      </div>
    
      <div class="modal-body">
        ...
      </div>
    
    
      <div class="modal-footer">
        <button type="button" class="btn btn-default"
            (click)="modalRef.hide()"
            >Close</button>
      </div>
    </ng-template>
    

    And finaly call the method to open the modal wherever you want like so:

    console.log(this.editSomethingModal);
    this.openModal( this.editSomethingModal );
    

    this.editSomethingModal is a TemplateRef that could be shown by the ModalService.

    Et voila! The modal defined in your component template file is shown by a call from inside your component class implementation. In my case I used this to show a modal from inside an event handler.

    0 讨论(0)
  • 2020-12-05 04:55

    We can use jquery to open bootstrap modal.

    ngAfterViewInit() { 
          $('#scanModal').modal('show'); 
    }
    
    0 讨论(0)
提交回复
热议问题