简体   繁体   中英

How to load the modal on page load angular 6

I am trying to create the modal, which will load on page load in angular 6, its working fine on click method which will pass one argument

I tried with ngOnInit, but its void type not taking the argument

<ng-template #content let-c="close" let-d="dismiss">
        <div class="modal-header">
          <h4 class="modal-title" id="modal-basic-title">Select user</h4>
          <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
              <select >
                <option value="volvo">Rohait</option>
                <option value="saab">Anuj</option>
              </select>   
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-outline-dark" (click)="c('Save click')">Save</button>
        </div>
      </ng-template>

Launch demo modal

that I running using this

open(content) {
      this.modalService.open(content);
     }

But its not working here, its working on click method

If you are using Angular 8, first, you can declare this.

@ViewChild('content', { static: true }) content: TemplateRef<any>;

However, for those who are using Angular 7 and below, you should use this instead.

@ViewChild('content') content: TemplateRef<any>;

The above will allow you to access the content template reference variable within your Class.

And on your ngOnInit,

ngOnInit() {
  this.modalService.open(this.content);
}

And do remember to import ViewChild , as well as TemplateRef into your component.ts,

import { TemplateRef, ViewChild } from '@angular/core';

This should allow your modalService to open the modal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM