最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load the modal on page load angular 6 - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jul 11, 2019 at 6:03 kpjackkpjack 413 silver badges9 bronze badges 1
  • You can take one approach like - after your ponent is intialized, you can make the open() work like <templateVariable>.nativeElement.click(). This will essentially click the button and inject your logic in the DOM – user6202450 Commented Jul 11, 2019 at 6:07
Add a ment  | 

1 Answer 1

Reset to default 4

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 ponent.ts,

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

This should allow your modalService to open the modal.

发布评论

评论列表(0)

  1. 暂无评论