gpt4 book ai didi

angular - 尝试关闭时出现 NgbModal 错误

转载 作者:太空狗 更新时间:2023-10-29 19:30:01 39 4
gpt4 key购买 nike

我已经成功地将 NgbModal 集成到我的 Angular 2 应用程序中,我目前在模态中显示了另一个组件。

我遇到的问题是,一旦它出现并单击“关闭”,我就会收到以下错误消息:

Cannot read property 'close' of undefined

现在我一直在关注Components as content我查看了 HTML 并浏览了 Typescript,但我不确定我在这里真正遗漏了什么。

这是我的类型脚本文件:

import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { BugService } from '../service/bug.service';
import { Bug } from '../model/bug';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { BugDetailComponent } from '../../bug-detail/bug-detail.component';

@Component({
selector: 'bug-list',
templateUrl: './bug-list.component.html',
styleUrls: ['./bug-list.component.css']
})

export class BugListComponent implements OnInit {

private bugs: Bug[] = [];

constructor(private bugService: BugService, private cdRef: ChangeDetectorRef, private modalService: NgbModal) { }

ngOnInit() {
this.getAddedBugs();
}

getAddedBugs() {
this.bugService.getAddedBugs().subscribe(bug => {
this.bugs.push(bug);
this.cdRef.detectChanges();
},
err => {
console.error("unable to get added bug - ", err);
});
}

open() {
const modalRef = this.modalService.open(BugDetailComponent);
modalRef.componentInstance.name = 'World';
}
}

我导入 BugDetailComponent,然后在 open() 函数中引用它。当我在模式出现后单击关闭时,我看到了错误消息。

我的HTML如下:

<div class="modal-header" [id]="modalId">
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title">Hi there!</h4>
</div>
<div class="modal-body">
<p>Hello, {{name}}!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
</div>

有人可以解释一下为什么我会收到此错误并帮助我修复它吗?

最佳答案

请忽略,我已经解决了这个问题。 bug-detail.component.ts

缺少以下构造函数
constructor(public activeModal: NgbActiveModal) {}

关于angular - 尝试关闭时出现 NgbModal 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403828/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com