bootstrap-modal - 如何在打开内部模态时关闭 ng-Bootstrap 模态
问题描述
我在模态中有一个模态。我假设是堆叠模式,但没有用于堆叠模式的 ng-bootstrap 语法。
当我单击第一个模态的链接时,打开内部模态,但不关闭前一个模态,这就是我想要做的,
我创建了一个名为app-login的模态组件,它位于标题组件按钮内。
header.component.html
<li class="nav-item mt-1">
<app-login></app-login>
</li>
该模态的相关内容为:
login.component.html
<ng-template #content let-modal>
...
<small class="form-text text-muted text-left">
<app-recuperar></app-recuperar>
</small>
...
</ng-template>
<button class="btn btn-outline-dark text-light mt-1" (click)="openVerticallyCentered(content)"> Entrar
<i class="fas fa-sign-in-alt fa-fw text-light"></i>
</button>
登录组件.ts
import { Component, OnInit } from '@angular/core';
import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
closeResult: string;
constructor(private modalService: NgbModal) {}
openVerticallyCentered(content) {
this.modalService.open(content, { centered: true });
}
ngOnInit() {
}
}
app-recuperar是内部模式
recuperar.component.html
<ng-template #content let-modal>
...
<a href="#" (click)="openVerticallyCentered(content)"> Recuperar contraseña</a>
...
</ng-template>
<a href="#" (click)="openVerticallyCentered(content)"> Recuperar contraseña</a>
recuperar.component.ts
import { Component, OnInit } from '@angular/core';
import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
@Component({
selector: 'app-recuperar',
templateUrl: './recuperar.component.html',
styleUrls: ['./recuperar.component.scss']
})
export class RecuperarComponent implements OnInit {
closeResult: string;
constructor(private modalService: NgbModal) {}
openVerticallyCentered(content) {
this.modalService.open(content, { centered: true });
}
ngOnInit() {
}
}
一切正常,两种模式都显示了,但问题是第二个模型是为了恢复密码而完成的,所以 login.component 保持可见没有任何意义。内部模态只是放置在第一个模态上。
这个想法是当我点击recuperar.component.html的链接时,首先关闭activeModal并同时打开新的模式。类似的东西(我知道语法是错误的):
<a href="#" (click)="activeModal.close();openVerticallyCentered(content)"> Recuperar contraseña</a>
解决方案
也许您可以关闭模态并从父组件打开另一个模态。为此,您可以在关闭它时传递一个参数:
activeModal.close(true);
然后检查此值以打开header.component.ts中的另一个模式:
const modalRef = this.modalService.open(content, { centered: true });
modalRef.result.then((recuperar) => {
if (recuperar) {
this.modalService.open(RecuperarComponent, { centered: true })
}
}
在这种情况下,您不需要在recuperar.component.html<ng-template>
中添加标签,并且该组件应该在您的模块中。entryComponent
推荐阅读
- python - Python excel 单元格类型(数字,单词,日期,...)
- git - 具有两个存储库、不同文件夹的 Azure DevOps 构建管道
- react-native - 是否可以使用地图功能在同一视图中的其他组件上方显示组件的阴影?反应原生
- highcharts - 在保留类别的同时动态添加 Highchart 点
- c# - SharePoint 连接仅在运行 Fiddler 时有效?
- javascript - 带有 Firebase 的 CORS vue.js
- python-3.x - 如何在 Python 中为语法创建解析树
- python - 计算出现次数并与另一个列表进行比较
- sitecore - 在 Sitecore 中重建链接数据库
- javascript - 有没有办法将 POST 请求的响应发送给另一个客户端?