【问题标题】:ngx bootstrap open modal via routingngx bootstrap 通过路由打开模式
【发布时间】:2021-06-18 19:45:12
【问题描述】:

我有一个 ngx 引导模式,目前可以运行,并打开一个详细列表页面,其中包含我的每个列表的特定数据。但是,我想使用路由,以便可以通过直接 url 打开我的列表模式,并根据它的 id 填充列表特定的详细信息。例如 .../listings/listing/50 将打开列表模式并填充列表 id 50 的详细信息。

此时我什至无法通过直接 url 打开任何模式。

目前我通过点击链接打开每个模式

<a (click)="openDetailsModal(listing)" ...

还有我的ListingService

  openDetailsModal(listing: Listing): void {
    this.selectedListing = listing;
    this.bsModalRef = this.modalService.show(ListingDetailComponent, {class:'listingDetailModal'});
    this.bsModalRef.content.listing = this.selectedListing;
  }

此外,我目前也在使用 HttpClient 从我的数据库中获取我的所有列表

【问题讨论】:

    标签: angular angular-routing ngx-bootstrap ngx-bootstrap-modal


    【解决方案1】:

    在这种情况下,您可以使用标准引导模式。将模态标记添加到您的组件,然后通过路由加载它。我已将“显示”类添加到模式中,以便它立即显示。确保您的父组件上有一个路由器插座。 Here is a demo on Stackblitz.

    您的列表模式组件如下所示:

    import { Component, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
        selector: 'listing-dialog',
        template: `
        <div id="backdrop" class="modal-backdrop fade" [ngClass]="{ show: showModal }"></div>
        <div class="modal d-block fade" 
          [ngClass]="{ show: showModal }"
          (click)="onClose()"
          id="listing-dialog" 
          tabindex="-1" role="dialog" aria-labelledby="modalIdLabel">
            <div class="modal-dialog" role="document" (click)="onDialogClick($event)">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5>I was loaded via route</h5>
                        <button type="button"
                            class="close"
                            (click)="onClose()"
                            aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                      <p>Add some detail here.</p>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-primary" (click)="onClose()">Close</button>
                    </div>
                </div>
            </div>
        </div>
        `
    })
    export class ListingDialogComponent implements AfterViewInit {
    
        showModal = false;
    
        constructor(private router: Router) { }
    
        ngAfterViewInit() {
          this.showModal = true;
        }
    
        onClose() {
          this.showModal = false;
          //Allow fade out animation to play before navigating back
          setTimeout(
            () => this.router.navigate(['..']),
            100
          );
        }
    
        onDialogClick(event: UIEvent) {
          // Capture click on dialog and prevent it from bubbling to the modal background.
          event.stopPropagation();
          event.cancelBubble = true;
        }
    }
    

    您的托管组件应该是这样的:

    <a [routerLink]="['listing']" >Load Listing</a>
    <router-outlet></router-outlet>
    

    模块需要注册路由:

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule } from '@angular/forms';
    import { Routes, RouterModule } from '@angular/router';
    
    import { AppComponent } from './app.component';
    import { HelloComponent } from './hello.component';
    import { ListingDialogComponent } from './listing-dialog.component';
    
    const routes: Routes = [
      { path: 'listing', component: ListingDialogComponent }
    ]
    
    @NgModule({
      imports:      [ BrowserModule, FormsModule, RouterModule.forRoot(routes) ],
      declarations: [ AppComponent, HelloComponent, ListingDialogComponent ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
    

    我没有在演示中包含参数,但是您可以轻松地在模态中观察它们,然后执行任何必要的服务调用来填充模态。我在我的应用程序的很多地方都使用了这种方法。

    【讨论】:

    • 谢谢@Joel,我可以让它以这种方式工作。我尝试让模态淡入淡出,但我似乎无法让它工作,你知道我该怎么做吗?
    • 还有没有办法在背景上关闭(点击)模式?抱歉新手问题,对角度来说真的很新
    • @JDinar 我更新了答案和 StackBlitz 以展示如何让淡入淡出动画工作并在单击背景时关闭。
    • 又是一次了不起的工作,谢谢它完美的工作。在你回答之前,我尝试自己做,最后创建了一个完整的指令,以便能够单击背景关闭它。您的解决方案更加优雅。
    • 感谢您的回答,但请注意任何可能使用最新 Angular 版本看到此问题的人 this.showModal = true;应该在 ngOnInit 上,因为如果放在 afterviewinit 上,它会导致 angular 在检查更改异常后抛出一个属性。否则效果很好!
    猜你喜欢
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2018-03-30
    • 2020-06-27
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多