【问题标题】:How to trigger bootstrap modal popup after page loads in Angular 7如何在Angular 7中加载页面后触发引导模式弹出窗口
【发布时间】:2020-06-12 01:11:34
【问题描述】:

我正在尝试实现一个功能,当用户访问着陆页并在着陆页停留超过 15 秒时,模式会在大约 15 秒后加载。但是视图加载后什么都没有加载

HTML:

<button [hidden]="true" data-toggle="modal" data-target="#myModal"></button>
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-scrollable" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="myModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>

component.ts 文件

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

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit, AfterViewInit {
  @ViewChild('myModal') myModal:ElementRef;
  constructor() { }
  ngOnInit() { }
  ngAfterViewInit () {
    setTimeout(() =>{
      this.myModal.nativeElement.click();;
    }, 15000)
  }
}

【问题讨论】:

    标签: html angular bootstrap-4 angular7 modalpopup


    【解决方案1】:

    您的代码运行完美,您只是忘记将引用变量#myModal 添加到您的按钮。

    例子:

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

    StackBlitz 示例:

    https://stackblitz.com/edit/angular-open-modal-afterviewinit-5s

    【讨论】:

    • 但是我的系统上什么都没有出现...当我在控制台中记录它时,它显示 ngAfterViewInit 正在加载但仍然没有出现
    • 我在我的回答中添加了一个活生生的例子。
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2018-05-26
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多