Angular模态框:如何在模态框和页面中重用相同的HTML?
创始人
2024-10-28 15:31:45
0

可以通过Angular中的ng-template来解决此问题。我们可以在组件中定义一个ng-template,并重用它来渲染页面和模态框。

在组件的HTML文件中,我们首先定义一个ng-template,如下所示:


  

在页面中,我们可以使用ng-container和ngTemplateOutlet指令来重用此ng-template,如下所示:


在模态框中,我们还需要使用ng-template来定义模态框的内容。我们将模态框的内容包含在一个ng-template中,并在打开模态框时加载它。在模态框组件的HTML文件中,我们可以这样做:


  
Open Modal

在组件的ts文件中,我们需要通过ViewChild装饰器来获取ng-template元素的引用。我们可以这样做:

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

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent {
  @ViewChild('myTemplate') myTemplate: TemplateRef;
  @ViewChild('modalTemplate') modalTemplate: TemplateRef;

  // open modal method here
  openModal() {
    // load modal content here
  }
}

在openModal方法中,我们需要通过模态框组件的服务加载模态框内容,如下所示:

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

constructor(private modalService: NgbModal) {}

openModal() {
  const modalRef = this.modal

相关内容

热门资讯

透视分享!wepoker破解器... 透视分享!wepoker破解器有用吗,wepoker私人局辅助挂-好像是真的有辅助软件(哔哩哔哩)1...
透视辅助!pokemomo辅助... 透视辅助!pokemomo辅助工具,pokernow辅助控制-竟然是真的有辅助软件(哔哩哔哩)1、下...
透视开挂!德普之星透视辅助软件... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视普及!wpk辅助是什么,w... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视曝光!wpk辅助器,wpk... 透视曝光!wpk辅助器,wpk软件是真的吗-切实有辅助技巧(哔哩哔哩)透视方法中分为三种模型:靠谱、...
透视普及!wepokerplu... 透视普及!wepokerplus万能挂,wepoker透视脚本免费下载-竟然真的是有辅助软件(哔哩哔...
透视推荐!wepoker辅助器... 透视推荐!wepoker辅助器最新版本更新内容,wepoker科技辅助器-本来是有辅助工具(哔哩哔哩...
透视普及!wepoker免费脚... 透视普及!wepoker免费脚本咨询,wepoker可以开透视吗-切实真的有辅助攻略(哔哩哔哩)1、...
透视曝光!wepoker辅助真... 透视曝光!wepoker辅助真的假的,wepoker辅助器安装包-确实真的是有辅助神器(哔哩哔哩)1...
透视解迷!aapoker怎么设... 透视解迷!aapoker怎么设置抽水,aapoker怎么开辅助器-原来一直总是有辅助app(哔哩哔哩...