如何打开作为子组件的 ng-Bootstrap-Modal?
How to open an ng-Bootstrap-Modal that is a child component?
我是 Angular 的新手,我对 Modals 和 ng-Bootstrap 有一些疑问.我已经能够打开同一组件中的模式,它们工作正常,一个例子是:
Link:
<a (click)="openAbout(contentAbout)" class="nav-link">About</a>
点击事件:
openAbout(contentAbout) {
this.modalService.open(contentAbout, { centered: true, scrollable: true });
}
模态:
<ng-template #contentAbout let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title" id="modal-primary-title">About us</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body centered">
<h2>Gravity Now!</h2>
<p>It is a platform designed for Space Apps Challenge and the Challenge Gravity Map, Earth Watch category and was chosen in the Top 5 of The Most Inspiring Projects of 2014.</p>
<a href="https://2014.spaceappschallenge.org/awards/#globalawards" target="_blank">
<img id="spaceLogo" src="./assets/space_apps.png">
</a>
<br>
<img id="supernovaLogo" src="./assets/supernova-logo.png">
<p>Also, you can download our apps.</p>
<div class="row">
<div class="col">
<a href="https://play.google.com/store/apps/details?id=tk.supernova.gnow" target="_blank">
<img class="logo" src="./assets/android.png">
</a>
</div>
<div class="col">
<a href="https://www.microsoft.com/en-us/p/gravity-now/9nblgggzjlp5" target="_blank">
<img class="logoWindows" src="./assets/windows.png">
</a>
</div>
</div>
</div>
</ng-template>
它有效,但是,我想将此模态移动到子组件,因为我有 4 个模态并且代码看起来有些混乱,但是如果我创建一个新的子组件,请将模态代码移动到child 然后,我将它添加到父组件,如下所示:
<app-about></app-about>
没有任何反应,因为点击没有打开任何东西。有没有人有类似的经历?你知道我应该在点击事件中更改什么吗?
我什至阅读了文档,但找不到与我的文档相关的示例:
https://ng-bootstrap.github.io/#/components/modal/examples
感谢任何想法。
我没试过这个,但你可以做的是你可以发出一个从 child 到 parent 的事件。
尝试在 Angular 文档中查找事件发射器。
Instead of calling child component in parent view you sould try this.
// if want to pass any value to child (parameter @Input() value in child);
// get back response from child (result @Output() EventEmitter value in child)
openChildComponentModel() {
const modalRef = this.modalService.open(
AboutComponent,
{backdrop: 'static'}
);
modalRef.componentInstance.parameter= 'Text';
modalRef.componentInstance.result.subscribe((response) => {}
});
}
有几个更改需要考虑。
第一步是在 app.module.ts
:
的 entryComponents
部分注册您的模态框
entryComponents: [
AboutComponent,
],
接下来,将旧模态框复制到新组件并删除这两行:
<ng-template #contentAbout let-c="close" let-d="dismiss">
</ng-template>
之后,点击事件的调用逻辑有一个小改动:
这是新的 HTML 代码:
<a class="nav-link" (click)="openAbout()">About</a>
这是新的 TypeScript 事件:
openAbout() {
//Here you define the name of your component
this.modalService.open(AboutComponent);
//This section is if you want to have any variable to initialize
//compConst.componentInstance.weight = undefined;
}
此外,在您的新组件中,您需要添加更改 constructor
并添加 NgbActiveModal
.
的实例
constructor(public activeModal: NgbActiveModal) { }
另外一个重要的变化是在关闭modal的逻辑上,需要改成这样:
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
您需要将旧的:(click)="d('Cross click')"
更改为 (click)="activeModal.dismiss('Cross click')"
有了所有这些更改,它就会像魅力一样发挥作用。我从这里得到了一些灵感:
我是 Angular 的新手,我对 Modals 和 ng-Bootstrap 有一些疑问.我已经能够打开同一组件中的模式,它们工作正常,一个例子是:
Link:
<a (click)="openAbout(contentAbout)" class="nav-link">About</a>
点击事件:
openAbout(contentAbout) {
this.modalService.open(contentAbout, { centered: true, scrollable: true });
}
模态:
<ng-template #contentAbout let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title" id="modal-primary-title">About us</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body centered">
<h2>Gravity Now!</h2>
<p>It is a platform designed for Space Apps Challenge and the Challenge Gravity Map, Earth Watch category and was chosen in the Top 5 of The Most Inspiring Projects of 2014.</p>
<a href="https://2014.spaceappschallenge.org/awards/#globalawards" target="_blank">
<img id="spaceLogo" src="./assets/space_apps.png">
</a>
<br>
<img id="supernovaLogo" src="./assets/supernova-logo.png">
<p>Also, you can download our apps.</p>
<div class="row">
<div class="col">
<a href="https://play.google.com/store/apps/details?id=tk.supernova.gnow" target="_blank">
<img class="logo" src="./assets/android.png">
</a>
</div>
<div class="col">
<a href="https://www.microsoft.com/en-us/p/gravity-now/9nblgggzjlp5" target="_blank">
<img class="logoWindows" src="./assets/windows.png">
</a>
</div>
</div>
</div>
</ng-template>
它有效,但是,我想将此模态移动到子组件,因为我有 4 个模态并且代码看起来有些混乱,但是如果我创建一个新的子组件,请将模态代码移动到child 然后,我将它添加到父组件,如下所示:
<app-about></app-about>
没有任何反应,因为点击没有打开任何东西。有没有人有类似的经历?你知道我应该在点击事件中更改什么吗?
我什至阅读了文档,但找不到与我的文档相关的示例:
https://ng-bootstrap.github.io/#/components/modal/examples
感谢任何想法。
我没试过这个,但你可以做的是你可以发出一个从 child 到 parent 的事件。 尝试在 Angular 文档中查找事件发射器。
Instead of calling child component in parent view you sould try this.
// if want to pass any value to child (parameter @Input() value in child);
// get back response from child (result @Output() EventEmitter value in child)
openChildComponentModel() {
const modalRef = this.modalService.open(
AboutComponent,
{backdrop: 'static'}
);
modalRef.componentInstance.parameter= 'Text';
modalRef.componentInstance.result.subscribe((response) => {}
});
}
有几个更改需要考虑。
第一步是在 app.module.ts
:
entryComponents
部分注册您的模态框
entryComponents: [
AboutComponent,
],
接下来,将旧模态框复制到新组件并删除这两行:
<ng-template #contentAbout let-c="close" let-d="dismiss">
</ng-template>
之后,点击事件的调用逻辑有一个小改动:
这是新的 HTML 代码:
<a class="nav-link" (click)="openAbout()">About</a>
这是新的 TypeScript 事件:
openAbout() {
//Here you define the name of your component
this.modalService.open(AboutComponent);
//This section is if you want to have any variable to initialize
//compConst.componentInstance.weight = undefined;
}
此外,在您的新组件中,您需要添加更改 constructor
并添加 NgbActiveModal
.
constructor(public activeModal: NgbActiveModal) { }
另外一个重要的变化是在关闭modal的逻辑上,需要改成这样:
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
您需要将旧的:(click)="d('Cross click')"
更改为 (click)="activeModal.dismiss('Cross click')"
有了所有这些更改,它就会像魅力一样发挥作用。我从这里得到了一些灵感: