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

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

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

14条回答
  •  旧时难觅i
    2020-12-05 05:03

    The below answer is in reference to the latest ng-bootstrap

    Install

    npm install --save @ng-bootstrap/ng-bootstrap
    

    app.module.ts

    import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
      declarations: [
        ...
      ],
      imports: [
        ...
    
        NgbModule
    
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    Component Controller

    import { TemplateRef, ViewChild } from '@angular/core';
    import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
    
    @Component({
      selector: 'app-app-registration',
      templateUrl: './app-registration.component.html',
      styleUrls: ['./app-registration.component.css']
    })
    
    export class AppRegistrationComponent implements OnInit {
    
      @ViewChild('editModal') editModal : TemplateRef; // Note: TemplateRef
    
      constructor(private modalService: NgbModal) { }
    
      openModal(){
        this.modalService.open(this.editModal);
      }
    
    }
    

    Component HTML

    
    
    
    
    
    
    
    
    
    

提交回复
热议问题