9

I have a modal wrapped in ng-template,

<ng-template #template>
  <div class="modal-header">
    <h4 class="modal-title pull-left">Modal for user id : {{ modalService.config.initialState.id }}</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">
    This is a modal.
  </div>
</ng-template>

i have a button to open this modal like

<button type="button" class="btn btn-primary" (click)="openModal(template)">Create template modal</button>

openModal(template: TemplateRef<any>) {
    const user = {
        id: 10
      };
    this.modalRef = this.modalService.show(template, {
      initialState : user
    });
}

this will work as i am passing template as a parameter.

How can i open this modal without passing the parameter? let it be i dont have a button, i want to open the modal on ngOninit. How it is possible?

stackblits

Diego Montania
  • 322
  • 5
  • 12
vishnuprasad kv
  • 990
  • 5
  • 22
  • 46
  • Possible duplicate of [Open ng-bootstrap modal programmatically](https://stackoverflow.com/questions/45133209/open-ng-bootstrap-modal-programmatically) – wentjun Apr 01 '19 at 07:03

4 Answers4

10

Get the reference of the template in typescript using @ViewChild() decorator and use ngAfterViewInit() hook to open the modal..

@ViewChild('template') templateRef: TemplateRef<any>;

ngAfterViewInit() {
  const user = {
      id: 10
    };
  this.modalRef = this.modalService.show(this.templateRef, {
    initialState : user
  });
}

https://stackblitz.com/edit/angular-modal-bootstap-pay2ua?file=app%2Fapp.component.ts

Edit

I just noticed that we would be getting an ExpressionChangedAfterItHasBeenCheckedError if we show it in ngAfterViewInit() hook, to fix that wrap opening the modal in a setTimeuut()

setTimeout(() => {
    this.modalRef = this.modalService.show(this.templateRef, {
      initialState : user
   })
})

https://stackblitz.com/edit/angular-modal-bootstap-pr377t?file=app/app.component.ts

Diego Montania
  • 322
  • 5
  • 12
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
2

There's a very simple method to open <ng-template> </ng-template> modal in angular.

You can use modalService to open the modal, add below in .ts-file .

step 1: import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

step 2: constructor(private modalService: NgbModal)

step 3:

function openModal(template) {
             this.modalService.open(template);
         }

When the button will be pressed, the function openModal() will be triggered and the 'template' parameter will pass as an argument. This in turn will trigger the modalService and open the modal 'template'.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • "When the button will be pressed, " - this is not the question being asked. The user want to know how to open the modal programmiclly. – Mason240 Jun 02 '21 at 15:18
  • The user already has mentioned "i have a button to open this modal like" , so the button click is the event to open the modal. – Shashank Kumar Jun 06 '22 at 06:22
1

You can use ViewChild decorator to get the reference and to load initially use AfterViewInit hook which called after Angular has fully initialized a component's view.

import { Component,TemplateRef, OnInit,ViewChild,ElementRef, AfterViewInit  } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit, AfterViewInit  {


  ngAfterViewInit() {
    const user = {
        id: 10
      };
      this.modalRef = this.modalService.show(this.template, {
      initialState : user
    });
  }

}

Demo

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

In Angular is simple way of ng-template modal from component

like bellow

import NgbModal :

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

export class YourComponent implements OnInit {
constructor(
  private modalService: NgbModal
 ) {}

 ngOnInit(): void {
   this.openModal('template');
 }

openModal(template) {
 this.modalService.open(template);
}
}
Siraj Ali
  • 526
  • 6
  • 13