【问题标题】:Popup Modal Dialog from Route Guard从 Route Guard 弹出模式对话框
【发布时间】:2017-09-26 17:17:56
【问题描述】:

从博文protecting routes using guards in angular 我们可以在用户无法访问路由时弹出一条消息。 :

import { CanDeactivate } from '@angular/router';
import { CanDeactivateComponent } from './app/can-deactivate';

export class ConfirmDeactivateGuard implements CanDeactivate<CanDeactivateComponent> {

  canDeactivate(target: CanDeactivateComponent) {
    if(target.hasChanges()){
        return window.confirm('Do you really want to cancel?');
    }
    return true;
  }
}

我想显示一个使用ngx-bootstrap 的模式对话框以允许用户登录。

我已经设法从主应用程序中获得了一些简单的代码加载模式(例如,不是来自我的app.component.ts 的子视图),例如

showloginModal() {
  this.loginModal.show();
}

模态是从另一个 HTML 文件加载的(我在选择器 app-login 中包含核心登录代码

<div class="modal-body">
  <app-login  #loginModal> </app-login>
</div>

编辑 我刚刚查看了Angular2 DialogService – Exploring Bootstrap,看起来它可以完成这项工作,但它适用于 bootstrap4

【问题讨论】:

  • 您打算在所有组件中使用它吗?
  • 来自我的路线守卫。
  • 你不能那样做。相反,您应该在 app.component 中处理它
  • 酷.....希望你明白
  • 好的。让我知道您是否需要任何帮助

标签: angular ngx-bootstrap


【解决方案1】:

ngx-bootstrap 无法做到这一点,但@ng-bootstrap/ng-bootstrap 可以。

Service 的用法示例

this.dialogService.show("Logged Out","You are out of here");

route guard 的用法示例

async canActivate() {
  if (!this.auth.loggedIn()) {
    await this.loginDialogService.confirm();
    return this.auth.loggedIn();
  }
  return true;
}

服务的源代码。

dialog.component.ts

import { Injectable }    from '@angular/core';
import {Component} from '@angular/core';
import {NgbModal, ModalDismissReasons,NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.css']
})
export class DialogComponent   {
    constructor(private modalService: NgbModal,public activeModal: NgbActiveModal) {}
    public title: string;
    public message: string;
}

@Injectable()
export class DialogService {  
  constructor(private modalService: NgbModal) {}

  public show(title: string, message:string) {
    const modalRef = this.modalService.open(DialogComponent);
    modalRef.componentInstance.title = title;
    modalRef.componentInstance.message = message;
    return modalRef.result;
  }
}

dialog.component.html

<div class="modal-header">
  <h5 class="modal-title" id="exampleModalLabel">{{title}}</h5>
  <button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="activeModal.close()">
          <span aria-hidden="true">&times;</span>
        </button>
</div>
<div class="modal-body">
  {{message}}
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-primary" data-dismiss="modal" (click)="activeModal.close()">Ok</button>
</div>

您还需要将其设为entryComponent 添加

entryComponents: [DialogComponent,LoginDialogComponent]

给你@NgModule

感谢long2know 不错的博客。

【讨论】:

  • 它可以与 bootstrap 3 一起使用吗?不用担心 css ..但是功能会起作用吗?
  • github.com/ng-bootstrap/ng-bootstrap 用于引导程序 4,但您可以为 bs3 编写代码
  • 任何其他支持promise方法的模态对话框......尝试使用可以激活类似于javascript确认的保护?
  • 未来的快速更新,如果您使用 Angular 9+,则无需将其添加到 entryComponents 数组中,因为它是 deprecated
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 2015-09-17
  • 2012-06-07
  • 2012-02-05
相关资源
最近更新 更多