简体   繁体   English

调用NgbModal open方法的最佳实践

[英]Best practice for calling the NgbModal open method

Toying around with the NgbModal and want to trigger the open method -> open(content: string | TemplateRef<any>, options: NgbModalOptions) <- from somewhere else than the template code. 使用NgbModal并且想要触发open方法 - > open(content: string | TemplateRef<any>, options: NgbModalOptions) < - 来自模板代码之外的其他地方。 In my case case I want to pass a string as a parameter when running the method in the .ts file of my component. 在我的情况下,我想在我的组件的.ts文件中运行方法时传递一个字符串作为参数。 When running the method via a button in the html file like so: <button (click)="open(content)">Launch demo modal</button> , the code works great, of course with all the code from within the <template></template> in the html file. 当通过html文件中的按钮运行方法时,如下所示: <button (click)="open(content)">Launch demo modal</button> ,代码效果很好,当然还有来自<template></template>所有代码html文件中的<template></template>

Trying to accomplish something with this: 试着用这个来完成一些事情:

logoutScreenOptions: NgbModalOptions = {
    backdrop: 'static',
    keyboard: false
};

lockedWindow: NgbModalRef;

lockedScreenContent= `
    <template #content let-c="close" let-d="dismiss">
        <div class="modal-header" style="text-align: center">
            <h3 class="modal-title">Title</h3>
        </div>
        <div class="modal-body">
            <p>Body</p>
        </div>
        <div class="modal-footer">
            <p>Footer</p>
        </div>
    </template>
`;

openLockedScreen(){
    this.open(this.lockedScreenContent);
}

open(content) {
    console.log(content);
    this.lockedWindow = this.modalService.open(content, this.logoutScreenOptions);
    this.lockedWindow.result.then((result) => {
        this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
        this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
}

Code runs with no errors, and the modal opens like so: Modal without rendered content ...which is not exactly what I want! 代码运行没有错误,模态打开如下: 模态没有渲染内容 ......这不是我想要的!

Also tried like this, with exactly the same result: 也尝试这样,结果完全相同:

lockedScreenContent= `
    <div class="modal-header" style="text-align: center">
        <h3 class="modal-title">Title</h3>
    </div>
    <div class="modal-body">
        <p>Body</p>
    </div>
    <div class="modal-footer">
        <p>Footer</p>
    </div>
`;

What am I missing? 我错过了什么? Wouldn't it be possible to just pass a string as the "content" parameter? 是不是可以只传递一个字符串作为“内容”参数?

Can't see to get my head around how to use a templateRef parameter from the ts file either! 无法看到如何使用ts文件中的templateRef参数!

As of today the open method of https://ng-bootstrap.github.io/#/components/modal has the following signature: open(content: string | TemplateRef<any>, options: NgbModalOptions) . 截至今天, https open(content: string | TemplateRef<any>, options: NgbModalOptions)open方法具有以下签名: open(content: string | TemplateRef<any>, options: NgbModalOptions) As you can see from this signature you can open a modal providing content as: 从这个签名中可以看出,您可以打开一个提供内容的模式:

  • string
  • TemplateRef

The string -typed argument is not very interesting - in fact it was mostly added to aid debugging / unit-testing. string式参数不是很有趣 - 实际上它主要是为了帮助调试/单元测试而添加的。 By using it you can pass just ... well, a piece of text , without any markup not Angular directives. 通过使用它你可以传递......好吧,一段文字,没有任何标记而不是Angular指令。 As such it is really a debug tool and not something that is useful in real-life scenarios. 因此,它实际上是一个调试工具,而不是在现实场景中有用的东西。

The TemplateRef argument is more interesting as it allows you to pass markup + directives to be displayed. TemplateRef参数更有趣,因为它允许您传递要显示的标记+指令。 You can get a hand on a TemplateRef by doing <template #refVar>...content goes here...</template> somewhere in your component template (a template of a component from which you plan to open a modal). 您可以通过<template #refVar>...content goes here...</template>在组件模板中的某个位置(计划打开模式的组件的模板)来获取TemplateRef As such the TemplateRef argument is powerful as it allows you to have markup, directives, other components etc. The downside is that TemplateRef is useful only if you are opening a modal from a component with a template. 因此, TemplateRef参数功能强大,因为它允许您使用标记,指令,其他组件等。缺点是只有在使用模板从组件中打开模态时, TemplateRef才有用。

I've got an impression that you are looking for the feature that is planned but not implemented yet - ability to open a modal with a component type as content. 我有一个印象,你正在寻找计划但尚未实现的功能 - 能够打开一个组件类型为内容的模态。 It would work as follows: modalService.open(MyComponentWithContent) . 它将按如下方式工作: modalService.open(MyComponentWithContent) As I've mentioned this is part of the roadmap but not implemented yet. 正如我所提到的,这是路线图的一部分,但尚未实施。 You can track this feature by following https://github.com/ng-bootstrap/ng-bootstrap/issues/680 您可以通过以下https://github.com/ng-bootstrap/ng-bootstrap/issues/680来跟踪此功能

You can now do the following... 您现在可以执行以下操作...

Lets say you have a model component Confirm model that you want to use it in any other component. 假设您有一个模型组件确认模型,您希望在任何其他组件中使用它。

  1. ModelComponentName is added to the declarations and entryComponent sections in the module.ts ModelComponentName被添加到module.ts中的声明和entryComponent部分
  2. Dont forget to add NgbModule.forRoot() in the imports of your module file which contains the declarations you mentioned above. 别忘了在包含上面提到的声明的模块文件的导入中添加NgbModule.forRoot()。
  3. Ensure that your model component template is inside div tag and not the ng-template tag. 确保模型组件模板位于div标签内,而不是ng-template标签。

You can then use the following open method from any other component. 然后,您可以从任何其他组件使用以下open方法。

modalReference: NgbModalRef;
constructor(private modalService: NgbModal) { }
this.modalReference = this.modalService.open(ModelComponentName, {backdrop: 'static',size: 'lg', keyboard: false, centered: true});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM